【问题标题】:No primary or default constructor found for Pageable in Pact Contract Provider test在 Pact Contract Provider 测试中找不到 Pageable 的主要或默认构造函数
【发布时间】:2019-05-06 02:52:13
【问题描述】:

我设置了以下协议合同提供者测试

@RunWith(SpringRestPactRunner.class)
@Provider("structures")
@PactFolder("pacts")
@VerificationReports({"console", "markdown"})
@SpringBootTest
public class ContractTest {

@MockBean
private MyServiceImpl myServiceImpl;

@Autowired
private MyController myController;

@Configuration
public static class TestConfiguration {
    @Bean
    public MyController myController() {
        return new MyController();
    }

}

@TestTarget
public final MockMvcTarget target = new MockMvcTarget();

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    target.setControllers(myController);
}

@State("My state")
public void setupDocumentWithStructures() {
    Mockito.when(myService.getStructuresByDocumentId(
            ArgumentMatchers.eq("1"),
            ArgumentMatchers.any()
    )).thenReturn(new PageImpl<>(Arrays.asList(
            Structure.of("first"),
            Structure.of("second")
    )));
}
}

运行测试结果:

java.lang.AssertionError: 
0 - Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable
java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable

方法 getStructuresByDocumentId 需要一个 Pageable 对象作为它的第二个参数。将注解 @SpringBootTest 更改为

@WebMvcTest(MyController.class)
@EnableSpringDataWebSupport

不能解决问题。有什么想法,如何解决这个问题?

【问题讨论】:

    标签: spring spring-mvc pact pact-jvm pact-java


    【解决方案1】:

    您在 setupDocumentWithStructures 中使用了“myService”,而您的 @MockBean 是 myServiceImpl ......我认为您的意思是在 setupDocumentWithStructures 中使用 myServiceImpl

    【讨论】:

      【解决方案2】:

      这就是它的工作原理

      @Before
      public void setupOrInit() {
            this.mockMvc = MockMvcBuilders.standaloneSetup(controller)
              .setControllerAdvice(new ErrorRequestInterceptor(tracer))
                  .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
              .build();
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-17
        • 2019-02-20
        • 2018-08-16
        • 2019-06-23
        • 2018-11-13
        • 2012-05-12
        • 2019-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多