SCC不支持Pageable?

时间:2017-02-15 13:45:44

标签: spring-cloud-contract

我正在使用spring cloud合约(1.1.0),但是当我想使用时,我完全陷入困境 可分页

  

http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Pageable.html

以下示例正常运行,没有可分页

@GetMapping("/employees")
@Timed
public List<EmployeeDTO> getAllEmployees() {
    log.debug("REST request to get all Employees");
    List<Employee> employees = employeeRepository.findAll();
    return employeeMapper.employeesToEmployeeDTOs(employees);
}

这个是使用可分页的

@GetMapping("/employees")
@Timed
public ResponseEntity<List<EmployeeDTO>> getAllEmployees(@ApiParam Pageable pageable)
    throws URISyntaxException {
    log.debug("REST request to get a page of Employees");
    Page<Employee> page = employeeRepository.findAll(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/employees");
    return new ResponseEntity<>(employeeMapper.employeesToEmployeeDTOs(page.getContent()), headers, HttpStatus.OK);
}

我的合同在这里。为第一个例子工作,而不是第二个

import org.springframework.cloud.contract.spec.Contract
[
Contract.make {
    name("get all")
    request {
        method 'GET'
        url('/api/employees?sort=id,desc')

    }
    response {
        status 200
        body([
            [id : 1,
            firstName : value(consumer('test'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            lastName : value(consumer('test'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            phoneNumber : value(consumer('test'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            salary : value(consumer(1000), producer(regex('[0-9]{1,10}'))),
            commissionPct : value(consumer(1000), producer(regex('[0-9]{1,10}')))
            ],
            [id : 2,
            firstName : value(consumer('test2'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            lastName : value(consumer('test2'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            phoneNumber : value(consumer('test2'), producer(regex('[a-zA-Z0-9]{1,255}'))),
            salary : value(consumer(2000), producer(regex('[0-9]{1,10}'))),
            commissionPct : value(consumer(2000), producer(regex('[0-9]{1,10}')))
        ]

        ])
        headers {
            contentType('application/json;charset=UTF-8')
        }
    }
}
]

堆栈跟踪:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:99)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:142)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:102)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:160)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    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:155)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.performRequest(MockMvcRequestSenderImpl.java:174)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.sendRequest(MockMvcRequestSenderImpl.java:404)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:582)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:79)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.get(MockMvcRequestSpecificationImpl.java:744)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.get(MockMvcRequestSpecificationImpl.java:66)
    at org.springframework.cloud.contract.verifier.tests.EmployeeTest.validate_get_all(EmployeeTest.java:47)

我该怎么办?我应该修改什么才能使它有效?

2 个答案:

答案 0 :(得分:1)

但你究竟想要实现什么目标?您在示例中展示的实际上是包含两个元素的单个实体。这是你想发送的吗?或者你想要第一个调用第一个元素然后第二个元素。如果是这种情况,则使用方案http://cloud.spring.io/spring-cloud-contract/1.0.x/#_scenarios执行有状态的情况。你需要做两份单独的合同。

另一个问题是,如果你真的需要测试分页是否有效。您想测试合同是否有效,并且仅通过发送请求一次(或者我想要测试您的测试?)。

更新:

我认为这与Spring Data + Rest Assured有关。请查看此问题 - Isolated Controller Test can't instantiate Pageable

答案 1 :(得分:0)

使用Pageable参数,Spring Data Rest可识别排序参数:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#paging-and-sorting.sorting

但在使用合同进行单元测试时,无法创建Pageable,因为它是一个界面...