jeudi 28 novembre 2019

java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable

I am working on Spring Boot REST and Mockito Test cases. In this example I am getting below error.

Error

0:30:37.547 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Failed to resolve argument 5 of type 'org.springframework.data.domain.Pageable'
java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:212)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:132)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:165)

Controller

@GetMapping("/{empId}/employees")
public ResponseEntity<Object> getemployees(@PathVariable(name = "empId") String empId,
        @RequestParam(required = false, name = "page") Integer page,
        @RequestParam(required = false, name = "size") Integer size,
        @RequestParam(defaultValue = "empId", name = "sortParam") String sortParam,
        @RequestParam(required = false) Direction direction,
        Pageable pageable) {

    Page<EmployeeDto> employeePage = employeeService.getDetails(empId, pageable, sortParam, direction);

    return new ResponseEntity<>(pagedResourcesAssembler.toResource(employeePage), HttpStatus.OK);
}

Test Case

@Test
public void testEmployees() throws Exception {
    Sort sort = new Sort(Sort.Direction.ASC, "billingNumber");
    Pageable pageable = PageRequest.of(0, 10, sort);

    when(employeeService.getDetails("1111", pageable, "A", Direction.ASC)).thenReturn(employeePages);

    mockMvc.perform(get("/employees/1111/employees?page=0&size=10&sortParam=billingNumber")
            .contentType(MediaType.APPLICATION_JSON))
            .andDo(print())
            .andExpect(status().isOk());
}



Aucun commentaire:

Enregistrer un commentaire