aspose file tools*
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
Author

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
[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

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing Spring Controller