MVN

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

Maven & Gradle

Nov 04, 2018
1k stars

pact-jvm-provider-junit5_2.11 · # Pact Junit 5 Extension ## Overview For writing Pact verification tests with JUnit 5, there is an JUnit 5 Invocation Context Provider that you can use with the `@TestTemplate` annotation. This will generate a test for each interaction found for the pact files for the provider. To use it, add the `@Provider` and one of the pact source annotations to your test class (as per a JUnit 4 test), then add a method annotated with `@TestTemplate` and `@ExtendWith(PactVerificationInvocationContextProvider.class)` that takes a `PactVerificationContext` parameter. You will need to call `verifyInteraction()` on the context parameter in your test template method. For example: ```java @Provider("myAwesomeService") @PactFolder("pacts") public class ContractVerificationTest { @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } } ``` For details on the provider and pact source annotations, refer to the [Pact junit runner](../pact-jvm-provider-junit/README.md) docs. ## Test target You can set the test target (the object that defines the target of the test, which should point to your provider) on the `PactVerificationContext`, but you need to do this in a before test method (annotated with `@BeforeEach`). There are three different test targets you can use: `HttpTestTarget`, `HttpsTestTarget` and `AmpqTestTarget`. For example: ```java @BeforeEach void before(PactVerificationContext context) { context.setTarget(HttpTestTarget.fromUrl(new URL(myProviderUrl))); // or something like // context.setTarget(new HttpTestTarget("localhost", myProviderPort, "/")); } ``` ## Provider State Methods Provider State Methods work in the same way as with JUnit 4 tests, refer to the [Pact junit runner](../pact-jvm-provider-junit/README.md) docs. ## Modifying the requests before they are sent **Important Note:** You should only use this feature for things that can not be persisted in the pact file. By modifying the request, you are potentially modifying the contract from the consumer tests! Sometimes you may need to add things to the requests that can't be persisted in a pact file. Examples of these would be authentication tokens, which have a small life span. The Http and Https test targets support injecting the request that will executed into the test template method. You can then add things to the request before calling the `verifyInteraction()` method. For example to add a header: ```java @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void testTemplate(PactVerificationContext context, HttpRequest request) { // This will add a header to the request request.addHeader("X-Auth-Token", "1234"); context.verifyInteraction(); } ``` ## Objects that can be injected into the test methods You can inject the following objects into your test methods (just like the `PactVerificationContext`). They will be null if injected before the supported phase. | Object | Can be injected from phase | Description | | ------ | --------------- | ----------- | | PactVerificationContext | @BeforeEach | The context to use to execute the interaction test | | Pact | any | The Pact model for the test | | Interaction | any | The Interaction model for the test | | HttpRequest | @TestTemplate | The request that is going to be executed (only for HTTP and HTTPS targets) | | ProviderVerifier | @TestTemplate | The verifier instance that is used to verify the interaction |

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-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-provider-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-provider-junit5_2.11 to add to Maven or Gradle - All Versions:

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

How to add a dependency to Maven

Add the following au.com.dius : pact-jvm-provider-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-provider-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-provider-junit5_2.11 gradle dependency to your build.gradle file:

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

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

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

How to add a dependency to SBT Scala

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

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

Advertisement