我有一个Spring MVC控制器,它使用Spring-Data的分页支持:
@Controller public class ModelController { private static final int DEFAULT_PAGE_SIZE = 50; @RequestMapping(value = "/models", method = RequestMethod.GET) public PageshowModels(@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转换?
注意:在实际应用中,一切正常.