I’m trying ot write an integration test for one of my methods, that handles a form submission.
The problem is that i use @PathVariable int id in the @RequestMapping of my controller.
I get the following error message when my test method reaches the .handle() part.
nested exception is java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping
So the controller cant reach the id. I’m using request.setRequestURI("/url-" + s.getId()); But appareantly its not helping with setting up @PathVariable.
Is there any way to set up the @PathVariable of my Mock object?
Update:
Yeah, im useing MockHttpServletRequest and annotationMethodHandlerAdapter.handle in my test class.
The Controller:
@RequestMapping(method = RequestMethod.POST, params = {"edit" })
public String onSubmit(@ModelAttribute("sceneryEditForm") SceneryEditForm s,
@PathVariable int id, Model model) {
// some code that useing id
}
The test method:
@Test
public void testEditButton() throws Exception {
MyObject s = new MyObject();
request.setMethod("POST");
request.setRequestURI("/edit-" + s.getId());
request.setParameter("edit", "set");
final ModelAndView mav = new AnnotationMethodHandlerAdapter()
.handle(request, response, controller);
assertViewName(mav, "redirect:/view-" + s.getId());
}
The error is correct: there is no path variable
idYou need to add the path expression with an placeholder
id