4

I'm trying to create a pact verification test with Pact/Junit5. This is the example I started with:

@Provider("myAwesomeService")
@PactFolder("pacts")
class MockMvcTestTargetStandaloneMockMvcTestJava {

    @BeforeEach
    void before(PactVerificationContext context) {
        MockMvcTestTarget testTarget = new MockMvcTestTarget();
        testTarget.setControllers(new DataResource());
        context.setTarget(testTarget);
    }
    @RestController
    static class DataResource {
        @GetMapping("/data")
        @ResponseStatus(HttpStatus.NO_CONTENT)
        void getData(@RequestParam("ticketId") String ticketId) {
        }
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }
}

The contract file is located in "src/test/resources/pacts"
but I'm getting this error:

14:53:30.452 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider - provideTestTemplateInvocationContexts called
14:53:30.456 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider - Verifying pacts for provider 'myAwesomeService' and consumer 'null'
14:53:30.456 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider - Pact source on test class: null
14:53:30.726 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider - Pact loaders on test class: []
java.lang.UnsupportedOperationException: At least one pact source must be present on the test class

Using Pact version 4.1.7

drone
  • 43
  • 5

2 Answers2

6

Double check that you're importing the right @PactFolder annotation.

import au.com.dius.pact.provider.junitsupport.loader.PactFolder;

I think this has caused issues due to an unfortunate naming conflict.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • 1
    yea, I was using `import au.com.dius.pact.core.model.annotations.PactFolder;` Thanks. – drone Jan 20 '21 at 16:19
0

I had the same error message in a project with messy pact dependencies. Different artefacts in different versions, i.e. 4.0.10 and 4.6.0 as well as some redundant dependencies (version number omitted):

testImplementation("au.com.dius", "pact-jvm-consumer-junit5")
testImplementation("au.com.dius.pact.consumer", "junit5")
testImplementation("au.com.dius.pact", "provider")
testImplementation("au.com.dius", "pact-jvm-provider")
testImplementation("au.com.dius", "pact-jvm-provider-junit5-spring")
...

For me, the solution was to use everywhere the same version (4.6.0) and limiting the dependencies to:

testImplementation("au.com.dius.pact.consumer", "junit5")
testImplementation("au.com.dius.pact.provider", "junit5spring")
testImplementation("au.com.dius.pact.provider", "junit5")
Marian
  • 2,571
  • 2
  • 9
  • 8