user525146
user525146

Reputation: 3998

Unit test annotation based Spring MVC Portlet controller

I am a newbie learning spring mvc with portlets. I have a controller which returns the view. I am not sure how to write the unit test that controller.

@controller
@RequestMapping("VIEW")
public class HelloController {

@ResourceMapping(value = "hello")
public String helloWorld(RenderRequest request) {

    return "hello";
}

and my Unit Test controller is something like this

public class HelloWorldControllerTest extends TestCase {

protected void setUp() throws Exception {
    super.setUp();
}

protected void tearDown() throws Exception {
    super.tearDown();
}

public void testHelloWorldController() throws IOException {

          MockRenderRequest request = new MockRenderRequest();
        HelloController c = new HelloController ();
        ModelAndView result = c.helloWorld(request);
        assertNotNull("ModelAndView should not be null", result);
        assertEquals("hello", result.getViewName());
}

This is not working as the result is not a ModelAndView object but it is a String in the controller. The return type can be a ModelAndView object in the main controller but if using spring annotation based then from the examples I have found the return type is String. Can anyone suggest which is the best practice or if I am wrong in understanding.

Thanks in advance

Upvotes: 0

Views: 1629

Answers (2)

markusf
markusf

Reputation: 313

Checkout spring-test-portlet-mvc (https://github.com/markusf/spring-test-portlet-mvc) to integration test your Spring controllers.

Upvotes: 0

Stefan Birkner
Stefan Birkner

Reputation: 24510

The spring-test-mvc project facilitates testing Spring MVC controllers.

Upvotes: 1

Related Questions