Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing Spring Controller

 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[code=java]
@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);


model.addAttribute("surveyControl",surveyControl);
model.addAttribute("somePackage",somePackage);

return "welcome";
}
[/code]

--here is my attempt at creating a unit test
[code]
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;

@Test
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);
}
}
[/code]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic