concept provider side in category java

appears as: provider side
Testing Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito

This is an excerpt from Manning's book Testing Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito.

Usually, each service is developed by a different team, making compatibility issues more complicated to detect if there isn’t good communication between teams. In our experience, the most common problems come from a change in the provider side so that the consumer can’t interact with the provider.

Listing 6.11. Testing the comments service on the provider side
@RunWith(Arquillian.class)
@ContractsFolder(value = "/tmp/mypacts")                                 #1
@Provider("comments_service")                                            #2
public class CommentsProviderTest {

    static {                                                             #3
        System.setProperty("MONGO_HOME",
                "/mongodb-osx-x86_64-3.2.7");
    }

    @ClassRule                                                           #4
    public static ManagedMongoDb managedMongoDb =
            newManagedMongoDbRule().build();

    @Rule                                                                #5
    public MongoDbRule remoteMongoDbRule = new MongoDbRule(mongoDb
            ().databaseName("test").host("localhost").build());

    @Deployment(testable = false)                                        #6
    public static WebArchive createDeployment() {
        final WebArchive webArchive = ShrinkWrap.create(WebArchive
                .class).addPackage(CommentsResource.class
                .getPackage()).addClass(MongoClientProvider.class)
                .addAsWebInfResource("test-resources.xml",
                        "resources.xml").addAsWebInfResource
                        (EmptyAsset.INSTANCE, "beans.xml")
                .addAsLibraries(Maven.resolver().resolve("org" +
                        ".mongodb:mongodb-driver:3.2.2")
                        .withTransitivity().as(JavaArchive.class));

        return webArchive;
    }

    private static final String commentObject = "{" + "  'comment' " +
            ": '%s'," + "  'rate' : %d," + "  'gameId': %d" + "}";

    @State("A game with id (\\d+) with rate (\\d+) and message (.+)")
    public void insertGame(int gameId, int rate, String message)
            throws MalformedURLException {                               #7

        RestAssured.given().body(toJson(String.format
                (commentObject, message, rate, gameId)))
                .contentType(ContentType.JSON).post(new URL
                (commentsService, "comments")).then().statusCode(201);

    }

    @ArquillianResource
    URL commentsService;

    @ArquillianResource                                                  #8
    Target target;

    @Test
    @UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)            #9
    public void should_provide_valid_answers() {
        PactProviderAssertions.assertThat(target).withUrl
                (commentsService).satisfiesContract();
    }

On the provider side, you need to replay the contract against a running provider. You can rely on the build script to package and deploy the provider application, or you can use Arquillian to package and deploy the application in the test, avoiding dependence on a build tool.