This module extends the base Pact JUnit5 module. See that for more details.
The combined library (JUnit5 + Spring) is available on maven central using:
group-id = au.com.dius.pact.provider artifact-id = junit5spring version-id = 4.2.x
For writing Spring Pact verification tests with JUnit 5, there is an JUnit 5 Invocation Context Provider that you can use with
@TestTemplate annotation. This will generate a test for each interaction found for the pact files for the provider.
To use it, add the
@ExtendWith(SpringExtension.class) and one of the pact source annotations to your test class (as per a JUnit 5 test), then
add a method annotated with
PactVerificationContext parameter. You will need to call
verifyInteraction() on the context parameter in
your test template method.
You will now be able to setup all the required properties using the Spring context, e.g. creating an application YAML file in the test resources:
You can also run pact tests against
MockMvc without need to spin up the whole application context which takes time
and often requires more additional setup (e.g. database). In order to run lightweight tests just use
from Spring and
MockMvcTestTarget as a test target before each test.
You can also use
MockMvcTestTarget for tests without spring context by providing the controllers manually.
@WebMvcTest starts only Spring MVC components you can't use
and need to fallback to
You can test Webflux routing functions using the
WebFluxTarget target class. The easiest way to do it is to get Spring to
autowire your handler and router into the test and then pass the routing function to the target.
As documented in Pact JUnit5 module, you can inject a request object to modify the requests made. However, depending on the Pact test target you are using, you need to use a different class.
|Test Target||Class to use|
|HttpTarget, HttpsTarget, SpringBootHttpTarget||org.apache.http.HttpRequest|