Download pact-jvm-provider-junit5 JAR 4.0.9 with all dependencies
# 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, "/"));
}
```
**Note for Maven users:** If you use Maven to run your tests, you will have to make sure that the Maven Surefire plugin is at least
version 2.22.1 uses an isolated classpath.
For example, configure it by adding the following to your POM:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
```
## 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.
### Using multiple classes for the state change methods
If you have a large number of state change methods, you can split things up by moving them to other classes. You will
need to specify the additional classes on the test context in a `Before` method. Do this with the `withStateHandler`
or `setStateHandlers` methods. See [StateAnnotationsOnAdditionalClassTest](src/test/java/au/com/dius/pact/provider/junit5/StateAnnotationsOnAdditionalClassTest.java) for an example.
## 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 |
## Allowing the test to pass when no pacts are found to verify (version 4.0.7+)
By default, the test will fail with an exception if no pacts were found to verify. This can be overridden by adding the
`@IgnoreNoPactsToVerify` annotation to the test class. For this to work, you test class will need to be able to receive
null values for any of the injected parameters.
Files of the artifact pact-jvm-provider-junit5 version 4.0.9 from the group au.com.dius.
Artifact pact-jvm-provider-junit5
Group au.com.dius
Version 4.0.9
Last update 05. April 2020
Tags: need they maven using stateannotationsonadditionalclasstest test verify junit5 beforeeach verifier plugin annotation modifying fail refer file pactverificationtesttemplate tokens only false verifyinteraction feature something groupid configure quot objects these template setstatehandlers isolated contractverificationtest going support moving interaction number like header defines myproviderport verification http them then will split each examples specify small auth different plugins before version executed life tests allowing token pactverificationcontext supported "null" pactverificationinvocationcontextprovider targets note just https classes files persisted void should extension point overview additional myawesomeservice extendwith method annotations usesystemclassloader public configuration methods users overridden sometimes change docs least exception injecting invocation apache same consumer used localhost settarget potentially takes requests sent were values apos would description request when three ignorenopactstoverify parameters instance sure work execute contract class classpath that 1234 surefire pact default example make this addheader phase large testtemplate span fromurl call model other generate have providerverifier withstatehandler state writing annotated myproviderurl readme calling object from artifactid following parameter details able pactfolder ampqtesttarget multiple receive with injected important your into inject provider junit httptesttarget pass which source authentication there target context uses java runner things adding pacts httpstesttarget dius httprequest found
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 3
Dependencies junit-jupiter-api, pact-jvm-core-support, pact-jvm-provider,
There are maybe transitive dependencies!
Group au.com.dius
Version 4.0.9
Last update 05. April 2020
Tags: need they maven using stateannotationsonadditionalclasstest test verify junit5 beforeeach verifier plugin annotation modifying fail refer file pactverificationtesttemplate tokens only false verifyinteraction feature something groupid configure quot objects these template setstatehandlers isolated contractverificationtest going support moving interaction number like header defines myproviderport verification http them then will split each examples specify small auth different plugins before version executed life tests allowing token pactverificationcontext supported "null" pactverificationinvocationcontextprovider targets note just https classes files persisted void should extension point overview additional myawesomeservice extendwith method annotations usesystemclassloader public configuration methods users overridden sometimes change docs least exception injecting invocation apache same consumer used localhost settarget potentially takes requests sent were values apos would description request when three ignorenopactstoverify parameters instance sure work execute contract class classpath that 1234 surefire pact default example make this addheader phase large testtemplate span fromurl call model other generate have providerverifier withstatehandler state writing annotated myproviderurl readme calling object from artifactid following parameter details able pactfolder ampqtesttarget multiple receive with injected important your into inject provider junit httptesttarget pass which source authentication there target context uses java runner things adding pacts httpstesttarget dius httprequest found
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 3
Dependencies junit-jupiter-api, pact-jvm-core-support, pact-jvm-provider,
There are maybe transitive dependencies!
There is a newer version: 4.0.10
Show newest versionShow all versions of pact-jvm-provider-junit5 Show documentation
Please rate this JAR file. Is it a good library?
0 downloads
Source code of pact-jvm-provider-junit5 version 4.0.9
META-INF
au.com.dius.pact.provider.junit5
© 2015 - 2024 Weber Informatics LLC | Privacy Policy