No primary or default constructor found for Pageable in Pact Contract Provider test

710 Views Asked by At

I set up following pact contract provider test

@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")
    )));
}
}

Running the test results in:

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

The method getStructuresByDocumentId expects a Pageable object as its second argument. Changing the annotation @SpringBootTest to

@WebMvcTest(MyController.class)
@EnableSpringDataWebSupport

Doesn't solve the problem. Any ideas, how to solve this issue?

3

There are 3 best solutions below

0
On

I was having the same problem and fixed setting a new mockMvc like this

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

I am not using @SpringBootTest as you are, but I think in this case it does not matter. Below is my entire (redacted) code.

@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
    }
}

I hope this helps, I spend a few hours trying to solve this.

Cheers

0
On

you used "myService" in your setupDocumentWithStructures whereas your @MockBean is myServiceImpl.......I think you meant to use myServiceImpl in setupDocumentWithStructures

0
On

That's how it can work

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