File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Testing Spring Controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Testing Spring Controller" Watch "Testing Spring Controller" New topic

Testing Spring Controller

Charles Owen
Ranch Hand

Joined: Aug 31, 2009
Posts: 61
I've created a controller that works, now I am just trying to test out the functionality with unit tests. I'm having some problems that I cannot resolve. I get a NullPointerException when I call the welcome method in the SurveyController. It seems to fail to create the SurveyControl domain object in the unit test. However, when I run the actual code it works. This is my first attempt at writing a unit test for a Spring controller, so I really don't know what I am doing.

this is the controller method--two different services are being called in order to populate the surveyControl and ncsPackage
@RequestMapping(value="/", method=RequestMethod.POST)
public String welcome(@RequestParam(required=true, value="solicitation") String solicitation,

ModelMap model)

SurveyControl surveyControl= surveyControlService.getSurveyControl(new Long(1));
SomePackage somePackage = somePackageService.getSomePackage(solicitation);


return "welcome";

--here is my attempt at creating a unit test
import org.springframework.ui.ModelMap;
import org.junit.Test;
import org.springframework.mock.web.*;
import static org.junit.Assert.*;

public class SurveyControllerTest
private static final String SOLICITATION_NUMBER = "xxxxxxxx";

SurveyController surveyController;

public void welcomeShouldReturnCorrectView()
surveyController = new SurveyController();
MockHttpServletRequest request = new MockHttpServletRequest("POST","/");
request.setSession(new MockHttpSession(null));
MockHttpServletResponse response = new MockHttpServletResponse();
ModelMap model = new ModelMap();
String viewName = surveyController.welcome(SOLICITATION_NUMBER,model);

System.out.println("THIS IS THE VIEW NAME" + viewName);
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Are you writing a unit test or an integration test.

If you are writing a unit test, then you don't need to mock out the request or response, since the Controller with annotations is just a POJO and you can test the code out just like you test any other Java class.

If you are writing an integration test, then yes you can mock out the request and response, but you also have to create a ModelMap object.

I am not sure why your code tags aren't working in your post.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Your code tags aren't working because you have the disable BB code in this message is checked. code tages are BB code, and now you disabled them and making your code unreadable, no indentations.

I agree. Here's the link:
subject: Testing Spring Controller
It's not a secret anymore!