Spring@PathVariable集成测试

Spring@PathVariable集成测试,spring,testing,spring-mvc,mocking,integration-testing,Spring,Testing,Spring Mvc,Mocking,Integration Testing,我正在尝试为我的一个方法编写一个集成测试,用于处理表单提交。 问题是我在控制器的@RequestMapping中使用了@PathVariable int id。 当我的测试方法到达.handle()部分时,我收到以下错误消息 因此控制器无法访问id。我使用的是request.setRequestURI(“/url-”+s.getId())但显然它对设置@PathVariable没有帮助。 有没有办法设置我的模拟对象的@PathVariable 更新: 是的,我在测试类中使用了MockHttpSe

我正在尝试为我的一个方法编写一个集成测试,用于处理表单提交。 问题是我在控制器的
@RequestMapping
中使用了
@PathVariable int id
。 当我的测试方法到达.handle()部分时,我收到以下错误消息

因此控制器无法访问id。我使用的是
request.setRequestURI(“/url-”+s.getId())
但显然它对设置
@PathVariable
没有帮助。 有没有办法设置我的模拟对象的
@PathVariable

更新:

是的,我在测试类中使用了
MockHttpServletRequest
annotationmethodhandleadapter.handle

控制员:

@RequestMapping(method = RequestMethod.POST, params = {"edit" })    
public String onSubmit(@ModelAttribute("sceneryEditForm") SceneryEditForm s,
            @PathVariable int id, Model model) {
       // some code that useing id
}
试验方法:

@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()); 
    }

错误是正确的:没有路径变量
id

@RequestMapping(value = "/something/{id}",
                method = RequestMethod.POST,
                params = {"edit" })
public String onSubmit(@ModelAttribute("sceneryEditForm") SceneryEditForm s,
        @PathVariable int id, Model model) {
   // some code that useing id
}
您需要添加带有占位符
id

@RequestMapping(value = "/something/{id}",
                method = RequestMethod.POST,
                params = {"edit" })
public String onSubmit(@ModelAttribute("sceneryEditForm") SceneryEditForm s,
        @PathVariable int id, Model model) {
   // some code that useing id
}

你能发布你的方法的定义(源方法,而不是测试方法)吗?请发布你的控件和测试。我假设您在测试中使用的是MockHttpServletRequest和annotationMethodHandlerAdapter.handle。我已经用示例代码更新了问题,希望对您有所帮助。