Isolated Controller Test can't instantiate Pageable

Original answer:

The problem with pageable can be solved by providing a custom argument handler. If this is set you will run in a ViewResolver Exception (loop). To avoid this you have to set a ViewResolver (an anonymous JSON ViewResolver class for example).

mockMvc = MockMvcBuilders.standaloneSetup(controller)
            .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
            .setViewResolvers(new ViewResolver() {
                @Override
                public View resolveViewName(String viewName, Locale locale) throws Exception {
                    return new MappingJackson2JsonView();
                }
            })
            .build();

Updated (2020): It is not necessary to add the ViewResolver anymore.

Regarding the parallel answer: It does not solve the problem for the original question to have this test running without ApplicationContext and/or friends.


Just add @EnableSpringDataWebSupport for test. Thats it.


For spring boot simply adding the ArgumentResolvers solved for me:

From code which triggered the error:

this.mockMvc = MockMvcBuilders.standaloneSetup(weightGoalResource).build();

To this, which works:

this.mockMvc = MockMvcBuilders.standaloneSetup(weightGoalResource)
            .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
            .build();