Download all versions of spring JAR files with all dependencies
spring from group au.com.dius.pact.provider (version 4.1.23)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.1.23
Last update 24. June 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.1.23
Last update 24. June 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.1.22)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.1.22
Last update 05. June 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.1.22
Last update 05. June 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.2.6)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.2.6
Last update 23. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.2.6
Last update 23. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.1.21)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.1.21
Last update 23. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.1.21
Last update 23. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.2.5)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.2.5
Last update 09. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.2.5
Last update 09. May 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.2.4)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.2.4
Last update 14. April 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.2.4
Last update 14. April 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.1.20)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.1.20
Last update 11. April 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.1.20
Last update 11. April 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.2.3)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.2.3
Last update 28. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.2.3
Last update 28. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.1.19)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.1.19
Last update 28. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.1.19
Last update 28. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 8
Dependencies nio-multipart-parser, spring-boot-starter-test, spring-webmvc, spring-webflux, spring-test, javax.servlet-api, jackson-datatype-joda, junit,
There are maybe transitive dependencies!
spring from group au.com.dius.pact.provider (version 4.2.2)
# Pact Spring/JUnit runner
## Overview
Library provides ability to play contract tests against a provider using Spring & JUnit.
This library is based on and references the JUnit package, so see the [Pact JUnit 4](/provider/junit/README.md) or [Pact JUnit 5](/provider/junit5/README.md) providers for more details regarding configuration using JUnit.
Supports:
- Standard ways to load pacts from folders and broker
- Easy way to change assertion strategy
- Spring Test MockMVC Controllers and ControllerAdvice using MockMvc standalone setup.
- MockMvc debugger output
- Spring WebFlux Controllers and RouterFunctions
- Multiple @State runs to test a particular Provider State multiple times
- **au.com.dius.pact.provider.junit.State** custom annotation - before each interaction that requires a state change,
all methods annotated by `@State` with appropriate the state listed will be invoked.
**NOTE:** For publishing provider verification results to a pact broker, make sure the Java system property `pact.provider.version`
is set with the version of your provider.
## Example of MockMvc test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class ContractTest {
//Create an instance of your controller. We cannot autowire this as we're not using (and don't want to use) a Spring test runner.
@InjectMocks
private AwesomeController awesomeController = new AwesomeController();
//Mock your service logic class. We'll use this to create scenarios for respective provider states.
@Mock
private AwesomeBusinessLogic awesomeBusinessLogic;
//Create an instance of your controller advice (if you have one). This will be passed to the MockMvcTarget constructor to be wired up with MockMvc.
@InjectMocks
private AwesomeControllerAdvice awesomeControllerAdvice = new AwesomeControllerAdvice();
//Create a new instance of the MockMvcTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public final MockMvcTarget target = new MockMvcTarget();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(awesomeController);
target.setControllerAdvice(awesomeControllerAdvice);
}
@State("default", "no-data") // Method will be run before testing interactions that require "default" or "no-data" state
public void toDefaultState() {
target.setRunTimes(3); //let's loop through this state a few times for a 3 data variants
when(awesomeBusinessLogic.getById(any(UUID.class)))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.ONE))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.TWO))
.thenReturn(myTestHelper.generateRandomReturnData(UUID.randomUUID(), ExampleEnum.THREE));
}
@State("error-case")
public void SingleUploadExistsState_Success() {
target.setRunTimes(1); //tell the runner to only loop one time for this state
//you might want to throw exceptions to be picked off by your controller advice
when(awesomeBusinessLogic.getById(any(UUID.class)))
.then(i -> { throw new NotCoolException(i.getArgumentAt(0, UUID.class).toString()); });
}
}
```
## Example of Spring WebFlux test
```java
@RunWith(RestPactRunner.class) // Custom pact runner, child of PactRunner which runs only REST tests
@Provider("myAwesomeService") // Set up name of tested provider
@PactFolder("pacts") // Point where to find pacts (See also section Pacts source in documentation)
public class AwesomeRouterContractTest {
//Create a new instance of the WebFluxTarget and annotate it as the TestTarget for PactRunner
@TestTarget
public WebFluxTarget target = new WebFluxTarget();
//Create instance of your RouterFunction
public RouterFunction<ServerResponse> routerFunction
= new AwesomeRouter(new AwesomeHandler()).routes();
//Configure the WebFluxTarget with routerFunction
@Before
public void setup() {
target.setRouterFunction(routerFunction);
}
}
```
## Using Spring runners
You can use `SpringRestPactRunner` or `SpringMessagePactRunner` instead of the default Pact runner to use the Spring test annotations. This will
allow you to inject or mock spring beans. `SpringRestPactRunner` is for restful webapps and `SpringMessagePactRunner` is
for async message tests.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("pricing")
@PactBroker(protocol = "https", host = "${pactBrokerHost}", port = "443",
authentication = @PactBrokerAuth(username = "${pactBrokerUser}", password = "${pactBrokerPassword}"))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // This will replace the bean with a mock in the application context
@TestTarget
@SuppressWarnings(value = "VisibilityModifier")
public final Target target = new HttpTarget(8091);
@State("Product X010000021 exists")
public void setupProductX010000021() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X010000021");
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
@State("the product code X00001 can be priced")
public void theProductCodeX00001CanBePriced() throws IOException {
reset(productClient);
ProductBuilder product = new ProductBuilder()
.withProductCode("X00001");
when(productClient.find((Set<String>) argThat(contains("X00001")), any())).thenReturn(product);
}
}
```
### Using Spring Context Properties
The SpringRestPactRunner will look up any annotation expressions (like `${pactBrokerHost}`)
above) from the Spring context. For Springboot, this will allow you to define the properties in the application test properties.
For instance, if you create the following `application.yml` in the test resources:
```yaml
pactbroker:
host: "your.broker.local"
port: "443"
protocol: "https"
auth:
username: "<your broker username>"
password: "<your broker password>"
```
Then you can use the defaults on the `@PactBroker` annotation.
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker(
authentication = @PactBrokerAuth(username = "${pactbroker.auth.username}", password = "${pactbroker.auth.password}")
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
```
### Using a random port with a Springboot test
If you use a random port in a springboot test (by setting `SpringBootTest.WebEnvironment.RANDOM_PORT`), you need to set it to the `TestTarget`. How this works is different for JUnit4 and JUnit5.
#### JUnit4
You can use the
`SpringBootHttpTarget` which will get the application port from the spring application context.
For example:
```java
@RunWith(SpringRestPactRunner.class)
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@TestTarget
public final Target target = new SpringBootHttpTarget();
}
```
#### JUnit5
You actually don't need to dependend on `pact-jvm-provider-spring` for this. It's sufficient to depend on `pact-jvm-provider-junit5`.
You can set the port to the `HttpTestTarget` object in the before method.
```java
@Provider("My Service")
@PactBroker
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PactVerificationTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
}
```
Artifact spring
Group au.com.dius.pact.provider
Version 4.2.2
Last update 13. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Group au.com.dius.pact.provider
Version 4.2.2
Last update 13. March 2021
Tags: need using passed junit4 junit5 runwith particular supports visibilitymodifier springboottest time defaults username references providers beans awesomeroutercontracttest variants autowire quot host mockmvc pricing system load each uuid ways yaml singleuploadexistsstate_success before data states folders spring scenarios easy times dependend loop tell notcoolexception ability look https define bean awesomehandler cannot routerfunction works replace picked controllers initmocks overview setcontrollers ioexception string documentation debugger library myawesomeservice exists testing annotations runs public configuration advice publishing methods resources getbyid change setupproductx010000021 awesomerouter localhost settarget serverresponse mockmvctarget instance initialize x010000021 pactrunner routerfunctions code logic that mockitoannotations setcontrolleradvice against pactbrokerauth require case example make exampleenum testtarget invoked annotated 8091 awesomecontrolleradvice through tostring tested mocking setting child webapps throw random_port following springrestpactrunner pactbrokeruser name pactfolder above multiple interactions protocol depend injectmocks provides provider restful junit assertion annotate message which source authentication service java pacts awesomecontroller test generaterandomreturndata package beforeeach springboot sufficient annotation getargumentat randomuuid reset only configure argthat listed fetch final awesomebusinesslogic random interaction like regarding create setrouterfunction productclient verification routes then will auth different port might springmessagepactrunner version private exceptions pactbrokerhost tests respective pactverificationcontext httptarget wired note want mock void webflux framework product point pactbrokerpassword method thenreturn broker based setruntimes contains actually where defined_port webfluxtarget password priced theproductcodex00001canbepriced more apos results when three pactbroker value contracttest controlleradvice custom sure appropriate contract throws class property properties requires localserverport expressions default pact webenvironment todefaultstate runners controller rest output this allow mockbean have restpactrunner state readme from withproductcode object standalone springboothttptarget details setup suppresswarnings mytesthelper find constructor with application error standard your inject play strategy async section httptesttarget mocks pricingserviceproviderpacttest x00001 pactverificationtest instead target context runner productbuilder local also dius
Organization not specified
URL https://github.com/DiUS/pact-jvm
License Apache 2
Dependencies amount 1
Dependencies junit,
There are maybe transitive dependencies!
Page 12 from 15 (items total 145)