Take the 2-minute tour ×

Let's imagine a Spring 3.1 web mvc application using MySql via Hibernate JPA via DAO's.

One has to write tests for the controllers without deploying the application and using an in-memory database (like hsqldb:mem for example). In other words, one should be able to run tests on a local PC.

How can this be achieved? Can someone provide a detailed example of how to write tests for such a controller? How should @ContextConfiguration be configured? How to retrieve a WebApplicationContext for testing purposes locally? Should one maintain a hibernate-test.cfg.xml?


I am not talking only about unit testing, I am talking about integration testing.


To make this complete on top of Sean Patrick Floyd's answer, here is a solution inspired from here:

Example of service:

public interface MarkingService {
    public String markItem(String item);

Example of controller using service

public class TestableController {

    private MarkingService markerService;

    @RequestMapping(value = "mark/{name}")  
    public String mark(Model ui, @PathVariable String name){  

        String value = this.markerService.markItem(name);
        ui.addAttribute("mark-value", value);

        return "mark-show";  



Mock of service to test controller:

public class MarkingServiceMock implements MarkingService {

    public String markItem(String item) {
        return "mockValue";


Config object for test configuration:

public class TestableControllerConfig {

    // We return a mock to test the controller layer
    public MarkingService markingService() {
        return new MarkingServiceMock();


Controller test:

public class TestableControllerTest {

    // Will be injected with TestableControllerConfig.markingService()
    private MarkingService markerService;

    public void testController() {

        // ...



Assuming the service layer relies on DAO's, it is easy to inject implementations based on in-memory db too.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Usually, you wouldn't test the whole setup. Write tests for the web layer and mock the persistence layer in them. Write separate tests for the service and persistence layer.

If you are going to test web controllers, use ModelAndViewAssert or the Mock versions of HttpServletRequest etc.

If on the other hand you want to test the whole setup, run automated web tests using HtmlUnit, Selenium etc., but do it on your real deployed app (of course on a non-public installation).

But in my opinion, if you want to test the big picture, you need to test the real thing. Mocking on a large scale gets very messy.

share|improve this answer
Ok, let's assume I separate web layer and persistence layer testing. And, let's assume I focus on the web layer testing, some controllers will still try to persist objects (not in my example, but I mean in most application they would). Are you saying I should have a separate hibernate config for my in memory database? If yes, how do I notify the tests to use this configuration? –  JVerstry Jul 10 '12 at 12:55
@JVerstry "some controllers will still try to persist objects" and that is exactly what makes an app untestable: when one layer does more than it should. the web layer should not be aware of persistence. it should use the service layer to do that. Now you just need to mock your service layer to test your web controllers, mock your persistence layer to test the service layer etc. Test one layer and mock the next. –  Sean Patrick Floyd Jul 10 '12 at 13:00
add comment

Your Answer


