name: au.com.dius:pact-jvm-provider_2.12
description: Pact provider ============= sub project of https://github.com/DiUS/pact-jvm The pact provider is responsible for verifying that an API provider adheres to a number of pacts authored by its clients This library provides the basic tools required to automate the process, and should be usable on its own in many instances. Framework and build tool specific bindings will be provided in separate libraries that build on top of this core functionality. ### Provider State Before each interaction is executed, the provider under test will have the opportunity to enter a state. Generally the state maps to a set of fixture data for mocking out services that the provider is a consumer of (they will have their own pacts) The pact framework will instruct the test server to enter that state by sending: POST &amp;quot;${config.stateChangeUrl.url}/setup&amp;quot; { &amp;quot;state&amp;quot; : &amp;quot;${interaction.stateName}&amp;quot; } ### An example of running provider verification with junit This example uses Groovy, JUnit 4 and Hamcrest matchers to run the provider verification. As the provider service is a DropWizard application, it uses the DropwizardAppRule to startup the service before running any test. **Warning:** It only grabs the first interaction from the pact file with the consumer, where there could be many. (This could possibly be solved with a parameterized test) ```groovy class ReadmeExamplePactJVMProviderJUnitTest { @ClassRule public static TestRule startServiceRule = new DropwizardAppRule&amp;lt;DropwizardConfiguration&amp;gt;( TestDropwizardApplication.class, ResourceHelpers.resourceFilePath(&amp;quot;dropwizard/test-config.yaml&amp;quot;)) private static ProviderInfo serviceProvider private static Pact&amp;lt;RequestResponseInteraction&amp;gt; testConsumerPact private static ConsumerInfo consumer @BeforeClass static void setupProvider() { serviceProvider = new ProviderInfo(&amp;quot;Dropwizard App&amp;quot;) serviceProvider.setProtocol(&amp;quot;http&amp;quot;) serviceProvider.setHost(&amp;quot;localhost&amp;quot;) serviceProvider.setPort(8080) serviceProvider.setPath(&amp;quot;/&amp;quot;) consumer = new ConsumerInfo() consumer.setName(&amp;quot;test_consumer&amp;quot;) consumer.setPactSource(new UrlSource( ReadmeExamplePactJVMProviderJUnitTest.getResource(&amp;quot;/pacts/zoo_app-animal_service.json&amp;quot;).toString())) testConsumerPact = PactReader.loadPact(consumer.getPactSource()) as Pact&amp;lt;RequestResponseInteraction&amp;gt; } @Test void runConsumerPacts() { // grab the first interaction from the pact with consumer Interaction interaction = testConsumerPact.interactions.get(0) // setup the verifier ProviderVerifier verifier = setupVerifier(interaction, serviceProvider, consumer) // setup any provider state // setup the client and interaction to fire against the provider ProviderClient client = new ProviderClient(serviceProvider, new HttpClientFactory()) Map&amp;lt;String, Object&amp;gt; failures = new HashMap&amp;lt;&amp;gt;() verifier.verifyResponseFromProvider(serviceProvider, interaction, interaction.getDescription(), failures, client) if (!failures.isEmpty()) { verifier.displayFailures(failures) } // Assert all good assertThat(failures, is(empty())) } private ProviderVerifier setupVerifier(Interaction interaction, ProviderInfo provider, ConsumerInfo consumer) { ProviderVerifier verifier = new ProviderVerifier() verifier.initialiseReporters(provider) verifier.reportVerificationForConsumer(consumer, provider) if (!interaction.getProviderStates().isEmpty()) { for (ProviderState providerState: interaction.getProviderStates()) { verifier.reportStateForInteraction(providerState.getName(), provider, consumer, true) } } verifier.reportInteractionDescription(interaction) return verifier } } ``` ### An example of running provider verification with spock This example uses groovy and spock to run the provider verification. Again the provider service is a DropWizard application, and is using the DropwizardAppRule to startup the service. This example runs all interactions using spocks Unroll feature ```groovy class ReadmeExamplePactJVMProviderSpockSpec extends Specification { @ClassRule @Shared TestRule startServiceRule = new DropwizardAppRule&amp;lt;DropwizardConfiguration&amp;gt;(TestDropwizardApplication, ResourceHelpers.resourceFilePath(&amp;apos;dropwizard/test-config.yaml&amp;apos;)) @Shared ProviderInfo serviceProvider ProviderVerifier verifier def setupSpec() { serviceProvider = new ProviderInfo(&amp;apos;Dropwizard App&amp;apos;) serviceProvider.protocol = &amp;apos;http&amp;apos; serviceProvider.host = &amp;apos;localhost&amp;apos; serviceProvider.port = 8080 serviceProvider.path = &amp;apos;/&amp;apos; serviceProvider.hasPactWith(&amp;apos;zoo_app&amp;apos;) { pactSource = new FileSource(new File(ResourceHelpers.resourceFilePath(&amp;apos;pacts/zoo_app-animal_service.json&amp;apos;))) } } def setup() { verifier = new ProviderVerifier() } def cleanup() { // cleanup provider state // ie. db.truncateAllTables() } def cleanupSpec() { // cleanup provider } @Unroll def &amp;quot;Provider Pact - With Consumer #consumer&amp;quot;() { expect: verifyConsumerPact(consumer).empty where: consumer &amp;lt;&amp;lt; serviceProvider.consumers } private Map verifyConsumerPact(ConsumerInfo consumer) { Map failures = [:] verifier.initialiseReporters(serviceProvider) verifier.runVerificationForConsumer(failures, serviceProvider, consumer) if (!failures.empty) { verifier.displayFailures(failures) } failures } } ``` 
license_spdx: Apache-2.0
version: 3.6.2
spec_version: 1.0
download_url: https://repo1.maven.org/maven2/au/com/dius/pact-jvm-provider_2.12/3.6.2/pact-jvm-provider_2.12-3.6.2.jar
home_url: https://github.com/DiUS/pact-jvm
news_url: https://libraries.io/maven/au.com.dius:pact-jvm-provider_2.12/versions.atom
date: 2019-02-17
about_resource: NONE
libraries_io_url: https://libraries.io/maven/au.com.dius:pact-jvm-provider_2.12?number=3.6.2
vcs_tool: git
vcs_repository: https://github.com/pact-foundation/pact-jvm
vcs_branch: master
