我设置了以下契约合同提供者测试
@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