44

I have a Spring MVC Controller which uses Pagination Support of 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;
    }

}

And I'd like to test the RequestMapping using the nice Spring MVC Test Support. In order to keep these tests fast and isolated from all the other stuff going on, I do not want to create the complete 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());
    }

}

This approach works fine with other Controllers, that do not expect a Pageable, but with this one I get one of these nice long Spring stacktraces. It complains about not being able to instantiate 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

Question: How do I change my test so the magic No-Request-Parameter-To-Pageable conversion happens properly?

Note: In the actual application everything is working fine.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348

3 Answers3

53

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.

meistermeier
  • 7,942
  • 2
  • 36
  • 45
32

Just add @EnableSpringDataWebSupport for test. Thats it.

user2031839
  • 461
  • 4
  • 3
  • 5
    I added `@EnableSpringDataWebSupport` annotation to my test class, but this did not have any effect. Could you give some more details on how this is supposed to work? -1 for now – Bastian Voigt Nov 21 '17 at 09:18
  • Should be the correct answer. If your unit test is still failing check to see if the error returned is different. Could be you've moved on to a different bug. – K.Nicholas May 18 '18 at 15:42
  • why this works? Can you give more explanation so i can understand better how to overcome situations when it happens again? :-) – Catarina Nogueira Jun 08 '18 at 18:25
  • 1
    Adding @EnableSpringDataWebSupport to the test class doesn't solve it for me - still the same exception: `No primary or default constructor found for interface org.springframework.data.domain.Pageable`. Is this dependent on some package? – Dalibor Filus Jul 04 '18 at 12:20
  • Sooo.... I had to remove @DataJpaTest and move BackendApplication upper one level. – Dalibor Filus Jul 04 '18 at 13:21
  • not working for my case – Yu Tian Toby Dec 02 '21 at 11:16
14

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();
groo
  • 4,213
  • 6
  • 45
  • 69