• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Testing Spring Controller

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic