我有一个 Spring MVC 控制器,它使用 Spring-Data 的分页支持:
@Controller
public class ModelController {
private static final int DEFAULT_PAGE_SIZE = 50;
@RequestMapping(value = "/models", method = RequestMethod.GET)
public Page<Model> showModels(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable, @RequestParam(
required = false) String modelKey) {
//..
return models;
}
}
我想使用漂亮的 Spring MVC 测试支持来测试 RequestMapping。为了使这些测试保持快速并与所有其他事情隔离开来,我不想创建完整的 ApplicationContext:
public class ModelControllerWebTest {
private MockMvc mockMvc;
@Before
public void setup() {
ModelController controller = new ModelController();
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void reactsOnGetRequest() throws Exception {
mockMvc.perform(get("/models")).andExpect(status().isOk());
}
}
这种方法适用于其他不期望 Pageable 的控制器,但是通过这种方法,我得到了这些不错的长 Spring 堆栈跟踪之一。它抱怨无法实例化 Pageable:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface
at
.... lots more lines
问题:如何更改我的测试,以便神奇的 No-Request-Parameter-To-Pageable 转换正确发生?
注意:在实际应用中一切正常。