MVN

au.com.dius : pact-jvm-consumer-junit5_2.11

Maven & Gradle

Nov 04, 2018
1k stars

pact-jvm-consumer-junit5_2.11 · pact-jvm-consumer-junit5 ======================== JUnit 5 support for Pact consumer tests ## Dependency 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` ## Usage ### 1. Add the Pact consumer test extension to the test class. 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. ```java @ExtendWith(PactConsumerTestExt.class) class ExampleJavaConsumerPactTest { ``` ### 2. create a method annotated with `@Pact` that returns the interactions for the test 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. ```java @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(); } ``` ### 3. Link the mock server with the interactions for the test with `@PactTestFor` 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](src/test/java/au/com/dius/pact/consumer/junit5/ArticlesTest.java) and [MultiTest](src/test/groovy/au/com/dius/pact/consumer/junit5/MultiTest.groovy). 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). ```java @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 | #### Matching the interactions by provider name 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](src/test/java/au/com/dius/pact/consumer/junit5/ArticlesTest.java) for an example. #### Matching the interactions by method name 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](src/test/groovy/au/com/dius/pact/consumer/junit5/MultiTest.groovy) for an example. ### Injecting the mock server into the test You can get the mock server injected into the test method by adding a `MockServer` parameter to the test method. ```java @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. ## Unsupported The current implementation does not support tests with multiple providers. This will be added in a later release.

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-consumer-junit5_2.11</artifactId>
    <version>3.5.24</version>
</dependency>
Copy
pom.xml
Table Of Contents

Latest Version

Choose a version of au.com.dius : pact-jvm-consumer-junit5_2.11 to add to Maven or Gradle - Latest Versions:

  • Latest Stable: 3.5.24

All Versions

Choose a version of au.com.dius : pact-jvm-consumer-junit5_2.11 to add to Maven or Gradle - All Versions:

Version Vulnerabilities Updated
3.5.x
pact-jvm-consumer-junit5_2.11-3.5.24
pact-jvm-consumer-junit5_2.11-3.5.23
pact-jvm-consumer-junit5_2.11-3.5.22
pact-jvm-consumer-junit5_2.11-3.5.21
pact-jvm-consumer-junit5_2.11-3.5.20
pact-jvm-consumer-junit5_2.11-3.5.19
pact-jvm-consumer-junit5_2.11-3.5.18
pact-jvm-consumer-junit5_2.11-3.5.17
pact-jvm-consumer-junit5_2.11-3.5.16
pact-jvm-consumer-junit5_2.11-3.5.15

How to add a dependency to Maven

Add the following au.com.dius : pact-jvm-consumer-junit5_2.11 maven dependency to the pom.xml file with your favorite IDE (IntelliJ / Eclipse / Netbeans):

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-consumer-junit5_2.11</artifactId>
    <version>3.5.24</version>
</dependency>

How to add a dependency to Gradle

Gradle Groovy DSL: Add the following au.com.dius : pact-jvm-consumer-junit5_2.11 gradle dependency to your build.gradle file:

implementation 'au.com.dius:pact-jvm-consumer-junit5_2.11:3.5.24'

Gradle Kotlin DSL: Add the following au.com.dius : pact-jvm-consumer-junit5_2.11 gradle kotlin dependency to your build.gradle.kts file:

implementation("au.com.dius:pact-jvm-consumer-junit5_2.11:3.5.24")

How to add a dependency to SBT Scala

SBT Scala: Add the following au.com.dius : pact-jvm-consumer-junit5_2.11 sbt scala dependency to your build.sbt file:

libraryDependencies += "au.com.dius" % "pact-jvm-consumer-junit5_2.11" % "3.5.24"

Advertisement