在Pact Contract Provider测试中找不到Pageable的主构造函数或默认构造函数



我设置了以下约定合同提供商测试

@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

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

您在setupDocumentWithStructures中使用了"myService",而您的@MockBean是myServiceImpl。。。。。。。我想你是想在setupDocumentWithStructures 中使用myServiceImpl

这就是的工作方式

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

我遇到了同样的问题,并修复了设置新的mockMvc(如(的问题

@Before
public void before() {
MockitoAnnotations.initMocks(this);
target.setMockMvc(MockMvcBuilders.standaloneSetup(myController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build());
}

我没有像你那样使用@SpringBootTest,但我认为在这种情况下这无关紧要。下面是我的全部(经过编辑的(代码。

@RunWith(SpringRestPactRunner.class)
@Provider("my-provider")
@PactBroker(url = "https://pact-broker.my-compnay.com")
public class MyControllerProviderContractTest {
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Mock
private MyService myService;
@InjectMocks
private MyController myController = new MyController();
@Before
public void before() {
MockitoAnnotations.initMocks(this);
target.setMockMvc(MockMvcBuilders.standaloneSetup(myController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build());
}

@State("my state")
public void stateForMyMethod() {
//my mocks
}
}

我希望这能有所帮助,我花了几个小时试图解决这个问题。

干杯

相关内容

  • 没有找到相关文章

最新更新