Spring MVC 在协定协定提供程序测试中找不到可分页的主构造函数或默认构造函数

hivapdat  于 2022-12-23  发布在  Spring
关注(0)|答案(3)|浏览(158)

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

@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

解决不了问题。有什么想法,怎么解决这个问题?

0aydgbwb

0aydgbwb1#

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

e37o9pze

e37o9pze2#

这就是它的工作原理

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

ux6nzvsh3#

我遇到了同样的问题,并修复了这样设置新的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
    }
}

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

相关问题