Home > Blockchain >  The request is not being executed in rest controller test
The request is not being executed in rest controller test

Time:11-21

I have a test method for one of the mappings where I provide all required data for the test

@Test
public void getAllMessagesNoFilterNotByTag() throws Exception {
    List<MessageDTO> messageDTOS = Arrays.asList(new MessageDTO(MESSAGE1), new MessageDTO(MESSAGE2));
    when(messageService.getAllMessages("", false,
          PageRequest.of(0, 5, Sort.by("creationDate").descending()))).thenReturn(messageDTOS);
    mockMvc.perform(get("/api/message?filter=&bytag=false&page=0"))
          .andExpect(status().isOk())
          .andExpect(content().json(
            objectMapper.writeValueAsString(Arrays.asList(new MessageDTO(MESSAGE1), new MessageDTO(MESSAGE2)))));
        verify(messageService, times(1)).getAllMessages("", false,
          PageRequest.of(0, 5, Sort.by("creationDate").descending()));
}

Here is the controller itself

@GetMapping
public List<MessageDTO> getAllMessages(@RequestParam(required = false) String filter,
      @RequestParam(name = "bytag", required = false) Boolean findByTag,
      @PageableDefault(sort = {"creationDate"}, direction = Sort.Direction.DESC, size = 5) Pageable pageable) {
      return messageService.getAllMessages(filter, findByTag, pageable);
}

But after running it I get this exception about failing to create Pageble object

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No primary or single public constructor found for interface org.springframework.data.domain.Pageable - and no default constructor found either
    
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
        at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
        at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
        at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
        at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:183)
        at com.training.vueblog.controllers.MessageControllerTest.getAllMessagesNoFilterNotByTag(MessageControllerTest.java:62)
    Caused by: java.lang.IllegalStateException: No primary or single public constructor found for interface org.springframework.data.domain.Pageable - and no default constructor found either
        at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:250)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
        at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:144)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:170)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        ... 74 more

CodePudding user response:

The problem was solve by adding extra configuration for mockMvc

@BeforeEach
public void setup(){
  mockMvc = MockMvcBuilders.standaloneSetup(messageController)
    .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
    .build();
}
  • Related