JUnit 5 support for Pact consumer tests
The library is available on maven central using:
- group-id =
au.com.dius
- artifact-id =
pact-jvm-consumer-junit5_2.12
- version-id =
3.5.x
To write Pact consumer tests with JUnit 5, you need to add @ExtendWith(PactConsumerTestExt)
to your test class. This
replaces the PactRunner
used for JUnit 4 tests. The rest of the test follows a similar pattern as for JUnit 4 tests.
@ExtendWith(PactConsumerTestExt.class)
class ExampleJavaConsumerPactTest {
For each test (as with JUnit 4), you need to define a method annotated with the @Pact
annotation that returns the
interactions for the test.
@Pact(provider="test_provider", consumer="test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.given("test state")
.uponReceiving("ExampleJavaConsumerPactTest test interaction")
.path("/")
.method("GET")
.willRespondWith()
.status(200)
.body("{\"responsetest\": true}")
.toPact();
}
Then the final step is to use the @PactTestFor
annotation to tell the Pact extension how to setup the Pact test. You
can either put this annotation on the test class, or on the test method. For examples see
ArticlesTest and
MultiTest.
The @PactTestFor
annotation allows you to control the mock server in the same way as the JUnit 4 PactProviderRule
. It
allows you to set the hostname to bind to (default is localhost
) and the port (default is to use a random port). You
can also set the Pact specification version to use (default is V3).
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "ArticlesProvider", port = "1234")
public class ExampleJavaConsumerPactTest {
NOTE on the hostname: The mock server runs in the same JVM as the test, so the only valid values for hostname are:
hostname | result |
---|---|
localhost |
binds to the address that localhost points to (normally the loopback adapter) |
127.0.0.1 or ::1 |
binds to the loopback adapter |
host name | binds to the default interface that the host machines DNS name resolves to |
0.0.0.0 or :: |
binds to the all interfaces on the host machine |
If you set the providerName
on the @PactTestFor
annotation, then the first method with a @Pact
annotation with the
same provider name will be used. See ArticlesTest for
an example.
If you set the pactMethod
on the @PactTestFor
annotation, then the method with the provided name will be used (it still
needs a @Pact
annotation). See MultiTest for an example.
You can get the mock server injected into the test method by adding a MockServer
parameter to the test method.
@Test
void test(MockServer mockServer) {
HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/articles.json").execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(equalTo(200)));
}
This helps with getting the base URL of the mock server, especially when a random port is used.
The current implementation does not support tests with multiple providers. This will be added in a later release.