Integrating with Java

Pact testing a Camel/Spring Boot application - example

This example shows how to implement Provider-side contract testing of a REST API which has been implemented using Apache Camel/Red Hat Fuse on Spring Boot. This will retrieve a contract from the Pact Broker:

  1. The Consumer first defines the contract, using the appropriate client-side tooling (e.g. for Angular)
  2. Add the Pact JVM Provider dependency to the Provider project.
  3. Add a test class (*IT) to the Provider project. Use the annotations provided by pact-jvm-provider to download the relevant contract from a Pact Broker. Use the SpringRestPactRunner.class runner.
  4. Configure Maven Failsafe Plugin to run the contract test during the Maven integration-test phase and publish results to the Pact Broker.

First add dependencies to the POM:


Construct a contract test class:

  • Replace any components that talk to downstream applications (e.g. databases, etc) with Camel mock components
  • Configure the mock components to return appropriate fake data
  • Build a test case for each State in the Pact contract.

An example test class might look something like this:

        classes = CatalogueSearchApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class MyApplicationProviderContractIT {

    @EndpointInject(uri = "mock:bean:my-data-store")
    MockEndpoint mockMyDataStore;

    public final Target target = new SpringBootHttpTarget();

     * Implements what the application should return when the following Pact
     * interaction is executed:
     * "state name as defined in the pact contract"
    @State("state name as defined in the pact contract")
    public void lookupUsingQueryParams() throws Exception {
        // Configure the mock bean to return some fake data
        mockMyDataStore.whenAnyExchangeReceived(new MockDataStoreProcessor());

        // The application will function otherwise as normal,
        // returning a response back to the caller

    @State("another state from the pact contract")
    public void anotherTest() throws Exception {
        // ...

    class MockDataStoreProcessor implements Processor {
        public void process(Exchange exchange) throws Exception {
            exchange.getMessage().setBody("some fake data");

Finally configure the Maven Failsafe Plugin to run the test with specific properties to connect to the Pact Broker:

        <!-- Configure plugin to play nicely with Spring Boot -->
        <!-- -->

        <!-- Configure PACT to publish contract test results when finished -->

Then the test can be run using:

mvn clean verify