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 Vote
object 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.
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.
@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.
Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.
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 .
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With