Java
Unit Testing for Spring MVC Controllers
Once the services, DAO, and supporting classes are tested, it’s time for the controller. Controllers are often the hardest layer to test, so many developers (based on observation) reach for Selenium or, worse, test by hand. That works, but it makes exercising individual logic branches awkward and slow, and no one wants to wait for a browser to spin up before checking in code. The Spring MVC Test framework solves this by driving full controller logic through fast unit tests, and it has lived in Spring’s core ever since Spring 4.
Spring 4 MVC Hello World Tutorial Using Maven
In this tutorial we build the smallest useful Spring MVC application: a controller that reads a request parameter and renders it in a JSP view. It is the classic “Hello World”, but the wiring it shows (a DispatcherServlet, a bean configuration, and a view resolver) is the same wiring every larger Spring MVC application is built on. Note: This tutorial has been updated for Spring Framework 7. It was originally written in 2014 for Spring 4 and the javax.* servlet API; the configuration below now uses the jakarta.* namespace, drops the Eclipse-specific setup for a plain Maven command, and runs on Jakarta EE 11 (Tomcat 11+). We keep XML configuration and web.xml so every piece of the wiring stays visible. The same beans map one-to-one onto a @Configuration class if you prefer Java config.
How To Convert Byte[] Array To String in Java
It is common to convert a String into a byte[] array and back again. The catch is that how you convert depends on what the bytes actually are: human-readable text in some encoding, or arbitrary binary data such as the output of a cipher. Using the wrong approach silently corrupts your data. Note: This post has been updated to always specify a charset explicitly and to cover the binary-data case. The original advice — new String(bytes) — relies on the platform’s default charset, which makes the result differ from machine to machine, and it cannot round-trip binary data at all. Both pitfalls are addressed below.