Search code examples
pactpact-broker

Provider test integration with pact broker for Spring Boot junit5 + configuration in application properties


The pact-jvm-provider-spring states that for junit5 provider test, it is not required to use the spring library.

However, @PactBroker annotation depends on the system properties. Is there a way to get this working for application properties via the Spring Property Resolver. I tried to create something similar to SpringEnvironmentResolver.kt and used it in the context setup. But that did not work.

@Provider("api-provider-app")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class PactVerificationTest {
    @LocalServerPort
    private int port;

    @Autowired
    private Environment environment;

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void testTemplate(Pact pact, Interaction interaction, HttpRequest request,
                  PactVerificationContext context) {
        context.setTarget(new HttpTestTarget("localhost", port));
        context.setValueResolver(new SpringResolver(environment));
        context.verifyInteraction();
    }
}

I get the following error

Invalid pact broker host specified ('${pactbroker.host:}'). Please provide a valid host or specify the system property 'pactbroker.host'.

Update After some more searching found out that the setTarget was not working and that needs to be moved to @BeforeEach method.

@BeforeEach
void setContext(PactVerificationContext context) {
    context.setValueResolver(new SpringResolver(environment));
    context.setTarget(new HttpTestTarget("localhost", port));
}

The following snippet helped it work with @PactFolder annotation. But the @PactBroker with properties is still not working


Solution

  • There is a new module added to Pact-JVM that extends the JUnit5 support to allow values to be configured in the Spring Context. See https://github.com/DiUS/pact-jvm/tree/master/provider/pact-jvm-provider-junit5-spring. It will be released with the next version of Pact-JVM, which will be 4.0.7.