MVN

au.com.dius : pact-jvm-provider-junit5-spring

Maven & Gradle

Apr 18, 2020
1k stars

pact-jvm-provider-junit5-spring · # Pact Spring/JUnit5 Support This module extends the base [Pact JUnit5 module](../pact-jvm-provider-junit5). See that for more details. For writing Spring 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 `@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 `@TestTemplate` and `@ExtendWith(PactVerificationSpringProvider.class)` that takes a `PactVerificationContext` parameter. You will need to call `verifyInteraction()` on the context parameter in your test template method. For example: ```java @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @Provider("Animal Profile Service") @PactBroker public class ContractVerificationTest { @TestTemplate @ExtendWith(PactVerificationSpringProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } } ``` 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: ```yaml pactbroker: host: your.broker.host auth: username: broker-user password: broker.password ``` 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 `@WebMvcTest` from Spring and `MockMvcTestTarget` as a test target before each test. For example: ```java @WebMvcTest @Provider("myAwesomeService") @PactBroker class ContractVerificationTest { @Autowired private MockMvc mockMvc; @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } @BeforeEach void before(PactVerificationContext context) { context.setTarget(new MockMvcTestTarget(mockMvc)); } } ``` You can also use `MockMvcTestTarget` for tests without spring context by providing the controllers manually. For example: ```java @Provider("myAwesomeService") @PactFolder("pacts") class MockMvcTestTargetStandaloneMockMvcTestJava { @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } @BeforeEach void before(PactVerificationContext context) { MockMvcTestTarget testTarget = new MockMvcTestTarget(); testTarget.setControllers(new DataResource()); context.setTarget(testTarget); } @RestController static class DataResource { @GetMapping("/data") @ResponseStatus(HttpStatus.NO_CONTENT) void getData(@RequestParam("ticketId") String ticketId) { } } } ``` **Important:** Since `@WebMvcTest` starts only Spring MVC components you can't use `PactVerificationSpringProvider` and need to fallback to `PactVerificationInvocationContextProvider`

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-junit5-spring</artifactId>
    <version>4.0.10</version>
</dependency>
Copy
pom.xml
Table Of Contents

Latest Version

Choose a version of au.com.dius : pact-jvm-provider-junit5-spring to add to Maven or Gradle - Latest Versions:

  • Latest Stable: 4.0.10

All Versions

Choose a version of au.com.dius : pact-jvm-provider-junit5-spring to add to Maven or Gradle - All Versions:

Version Vulnerabilities Updated
4.0.x
pact-jvm-provider-junit5-spring-4.0.10
pact-jvm-provider-junit5-spring-4.0.9
pact-jvm-provider-junit5-spring-4.0.8
pact-jvm-provider-junit5-spring-4.0.7

How to add a dependency to Maven

Add the following au.com.dius : pact-jvm-provider-junit5-spring 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-spring</artifactId>
    <version>4.0.10</version>
</dependency>

How to add a dependency to Gradle

Gradle Groovy DSL: Add the following au.com.dius : pact-jvm-provider-junit5-spring gradle dependency to your build.gradle file:

implementation 'au.com.dius:pact-jvm-provider-junit5-spring:4.0.10'

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

implementation("au.com.dius:pact-jvm-provider-junit5-spring:4.0.10")

How to add a dependency to SBT Scala

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

libraryDependencies += "au.com.dius" % "pact-jvm-provider-junit5-spring" % "4.0.10"

Advertisement

Dependencies from Group

Apr 29, 2020
31 usages
1k stars
Nov 04, 2018
27 usages
1k stars
Nov 04, 2018
18 usages
1k stars
Apr 29, 2020
13 usages
1k stars
Apr 29, 2020
12 usages
1k stars

Discover Dependencies

Mar 10, 2020
2 stars
Mar 10, 2020
2 stars
Mar 10, 2020
2 stars
Feb 18, 2023
0 stars
Aug 25, 2023
7 usages
11.9k stars
Dec 08, 2022
1 usages
38 stars