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
public String welcome(@RequestParam(required=true, value="solicitation") String solicitation,
--here is my attempt at creating a unit test
import static org.junit.Assert.*;
public class SurveyControllerTest
private static final String SOLICITATION_NUMBER = "xxxxxxxx";
public void welcomeShouldReturnCorrectView()
surveyController = new SurveyController();
MockHttpServletRequest request = new MockHttpServletRequest("POST","/");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelMap model = new ModelMap();
String viewName = surveyController.welcome(SOLICITATION_NUMBER,model);
System.out.println("THIS IS THE VIEW NAME" + viewName);
Are you writing a unittest 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.