Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set @ModelAttribute in MockHttpServletRequest in JUnit Test

I'm trying to test a spring mvc controller. One of the methods takes a form input as a POST method. This method gets the form's commandObject through a @ModelAttribute annotation. How can I set this test case up, using Spring's Junit test?

The controller's method looks like this:

@RequestMapping(method = RequestMethod.POST)
public String formSubmitted(@ModelAttribute("vote") Vote vote, ModelMap model) { ... }

The Voteobject is defined in the .jsp:

 <form:form method="POST" commandName="vote" name="newvotingform">

Now I want to test this form POST in a Test which is set up as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@TestExecutionListeners({WebTestExecutionerListener.class, DependencyInjectionTestExecutionListener.class})
public class FlowTest { ... }

The actual method which is testing the form POST:

@Test
public void testSingleSession() throws Exception {

    req = new MockHttpServletRequest("GET", "/vote");
    res = new MockHttpServletResponse();
    handle = adapter.handle(req, res, vc);
    model = handle.getModelMap();

    assert ((Vote) model.get("vote")).getName() == null;
    assert ((Vote) model.get("vote")).getState() == Vote.STATE.NEW;

    req = new MockHttpServletRequest("POST", "/vote");
    res = new MockHttpServletResponse();

    Vote formInputVote = new Vote();
    formInputVote.setName("Test");
    formInputVote.setDuration(45);

    //        req.setAttribute("vote", formInputVote);
    //        req.setParameter("vote", formInputVote);
    //        req.getSession().setAttribute("vote", formInputVote);

    handle = adapter.handle(req, res, vc) ;
    model = handle.getModelMap();

    assert "Test".equals(((Vote) model.get("vote")).getName());
    assert ((Vote) model.get("vote")).getState() == Vote.STATE.RUNNING;
}

The 3 lines which are currently commented out, are feeble attempts to make this work - it did not work however. Can anyone provide some tips on this?

I don't really want to call the controllers method directly in my test as I feel like this would not really test the controller in a web context.

like image 296
chzbrgla Avatar asked Jun 14 '11 06:06

chzbrgla


People also ask

How do you write unit test cases for REST API in spring boot?

React + Spring Boot Microservices and Spring Spring Boot provides an easy way to write a Unit Test for Rest Controller file. With the help of SpringJUnit4ClassRunner and MockMvc, we can create a web application context to write Unit Test for Rest Controller file.

What is @ModelAttribute annotation in spring?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

Which annotation sets the Spring application context for JUnit test cases?

Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.

What annotation causes JUnit to let spring boot control the test?

Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication .


1 Answers

You have to simulate what your HTML form will do. It will simply pass string request parameters. Try:

req.setParameter("name","Test");
req.setParameter("duration","45");
like image 70
rjsang Avatar answered Oct 02 '22 15:10

rjsang