我按照契约合同提供者测试进行了设置
@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
不能解决问题。有什么想法,如何解决这个问题?
答案 0 :(得分:0)
您在setupDocumentWithStructures中使用了“ myService”,而您的@MockBean是myServiceImpl .......我想您打算在setupDocumentWithStructures中使用myServiceImpl
答案 1 :(得分:0)
这就是它的工作原理
@Before
public void setupOrInit() {
this.mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setControllerAdvice(new ErrorRequestInterceptor(tracer))
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build();
}