junit5spring

# Pact Spring/JUnit5 Support This module extends the base [Pact JUnit5 module](/provider/junit5/README.md). See that for more details. ## Dependency The combined library (JUnit5 + Spring) is available on maven central using: group-id = au.com.dius.pact.provider artifact-id = junit5spring version-id = 4.2.x ## Usage 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(PactVerificationSpringProvider.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(PactVerificationSpringProvider.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` ## Webflux tests You can test Webflux routing functions using the `WebFluxTarget` target class. The easiest way to do it is to get Spring to autowire your handler and router into the test and then pass the routing function to the target. For example: ```java @Autowired YourRouter router; @Autowired YourHandler handler; @BeforeEach void setup(PactVerificationContext context) { context.setTarget(new WebFluxTarget(router.route(handler))); } @TestTemplate @ExtendWith(PactVerificationSpringProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } ``` ## Modifying requests As documented in [Pact JUnit5 module](/provider/junit5/README.md#modifying-the-requests-before-they-are-sent), you can inject a request object to modify the requests made. However, depending on the Pact test target you are using, you need to use a different class. | Test Target | Class to use | |-------------|--------------| | HttpTarget, HttpsTarget, SpringBootHttpTarget | org.apache.http.HttpRequest | | MockMvcTestTarget | MockHttpServletRequestBuilder | | WebFluxTarget | WebTestClient.RequestHeadersSpec | # Verifying V4 Pact files that require plugins (version 4.3.0+) Pact files that require plugins can be verified with version 4.3.0+. For details on how plugins work, see the [Pact plugin project](https://github.com/pact-foundation/pact-plugins). Each required plugin is defined in the `plugins` section in the Pact metadata in the Pact file. The plugins will be loaded from the plugin directory. By default, this is `~/.pact/plugins` or the value of the `PACT_PLUGIN_DIR` environment variable. Each plugin required by the Pact file must be installed there. You will need to follow the installation instructions for each plugin, but the default is to unpack the plugin into a sub-directory `<plugin-name>-<plugin-version>` (i.e., for the Protobuf plugin 0.0.0 it will be `protobuf-0.0.0`). The plugin manifest file must be present for the plugin to be able to be loaded. # Test Analytics We are tracking anonymous analytics to gather important usage statistics like JVM version and operating system. To disable tracking, set the 'pact_do_not_track' system property or environment variable to 'true'.

License

License

Categories

Categories

IDE Development Tools JUnit Unit Testing
GroupId

GroupId

au.com.dius.pact.provider
ArtifactId

ArtifactId

junit5spring
Last Version

Last Version

4.4.0-beta.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

junit5spring
# Pact Spring/JUnit5 Support This module extends the base [Pact JUnit5 module](/provider/junit5/README.md). See that for more details. ## Dependency The combined library (JUnit5 + Spring) is available on maven central using: group-id = au.com.dius.pact.provider artifact-id = junit5spring version-id = 4.2.x ## Usage 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(PactVerificationSpringProvider.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(PactVerificationSpringProvider.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` ## Webflux tests You can test Webflux routing functions using the `WebFluxTarget` target class. The easiest way to do it is to get Spring to autowire your handler and router into the test and then pass the routing function to the target. For example: ```java @Autowired YourRouter router; @Autowired YourHandler handler; @BeforeEach void setup(PactVerificationContext context) { context.setTarget(new WebFluxTarget(router.route(handler))); } @TestTemplate @ExtendWith(PactVerificationSpringProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { context.verifyInteraction(); } ``` ## Modifying requests As documented in [Pact JUnit5 module](/provider/junit5/README.md#modifying-the-requests-before-they-are-sent), you can inject a request object to modify the requests made. However, depending on the Pact test target you are using, you need to use a different class. | Test Target | Class to use | |-------------|--------------| | HttpTarget, HttpsTarget, SpringBootHttpTarget | org.apache.http.HttpRequest | | MockMvcTestTarget | MockHttpServletRequestBuilder | | WebFluxTarget | WebTestClient.RequestHeadersSpec | # Verifying V4 Pact files that require plugins (version 4.3.0+) Pact files that require plugins can be verified with version 4.3.0+. For details on how plugins work, see the [Pact plugin project](https://github.com/pact-foundation/pact-plugins). Each required plugin is defined in the `plugins` section in the Pact metadata in the Pact file. The plugins will be loaded from the plugin directory. By default, this is `~/.pact/plugins` or the value of the `PACT_PLUGIN_DIR` environment variable. Each plugin required by the Pact file must be installed there. You will need to follow the installation instructions for each plugin, but the default is to unpack the plugin into a sub-directory `<plugin-name>-<plugin-version>` (i.e., for the Protobuf plugin 0.0.0 it will be `protobuf-0.0.0`). The plugin manifest file must be present for the plugin to be able to be loaded. # Test Analytics We are tracking anonymous analytics to gather important usage statistics like JVM version and operating system. To disable tracking, set the 'pact_do_not_track' system property or environment variable to 'true'.
Project URL

Project URL

https://github.com/DiUS/pact-jvm
Source Code Management

Source Code Management

https://github.com/pact-foundation/pact-jvm

Download junit5spring

Dependencies

compile (6)

Group / Artifact Type Version
au.com.dius.pact.provider : junit5 jar 4.4.0-beta.2
org.springframework : spring-context jar 5.3.9
org.springframework : spring-test jar 5.3.9
org.springframework : spring-web jar 5.3.9
org.springframework : spring-webflux jar 5.3.9
javax.servlet : javax.servlet-api jar 3.1.0

runtime (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.6.20
org.jetbrains.kotlin : kotlin-reflect jar 1.6.20
org.hamcrest : hamcrest jar 2.2
org.apache.commons : commons-lang3 jar 3.12.0
javax.mail : mail jar 1.5.0-b01

Project Modules

There are no modules declared in this project.
au.com.dius.pact.provider

DiUS Computing Pty Ltd

Versions

Version
4.4.0-beta.2
4.4.0-beta.1
4.4.0-beta.0
4.3.10
4.3.9
4.3.8
4.3.7
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.3.0-beta.6
4.3.0-beta.5
4.3.0-beta.4
4.3.0-beta.3
4.3.0-beta.2
4.3.0-beta.1
4.3.0-beta.0
4.2.21
4.2.20
4.2.19
4.2.18
4.2.17
4.2.16
4.2.15
4.2.14
4.2.13
4.2.12
4.2.11
4.2.10
4.2.9
4.2.8
4.2.7
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.2.0-beta.3
4.2.0-beta.2
4.2.0-beta.1
4.2.0-beta.0
4.1.38
4.1.37
4.1.36
4.1.35
4.1.34
4.1.33
4.1.32
4.1.31
4.1.30
4.1.29
4.1.28
4.1.27
4.1.26
4.1.25
4.1.24
4.1.23
4.1.22
4.1.21
4.1.20
4.1.19
4.1.18
4.1.17
4.1.16
4.1.15
4.1.14
4.1.13
4.1.12
4.1.11
4.1.10
4.1.9
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0