Group / Artifact / Version / pact-jvm-consumer-junit_2.10 / 2.1.7
Descriptionpact-jvm-consumer-junit ======================= Provides a DSL and a base test class for use with Junit to build consumer tests. ##Dependency The library is available on maven central using: * group-id = `` * artifact-id = `pact-jvm-consumer-junit_2.11` * version-id = `2.1.x` ##Usage ### Using the base ConsumerPactTest To write a pact spec extend ConsumerPactTest. This base class defines the following four methods which must be overridden in your test class. * *providerName:* Returns the name of the API provider that Pact will mock * *consumerName:* Returns the name of the API consumer that we are testing. * *createFragment:* Returns the PactFrament containing the interactions that the test setup using the ConsumerPactBuilder DSL * *runTest:* The actual test run. It receives the URL to the mock server as a parameter. Here is an example: ```java import; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; public class ExampleJavaConsumerPactTest extends ConsumerPactTest { @Override protected PactFragment createFragment(ConsumerPactBuilder.PactDslWithProvider builder) { Map headers = new HashMap(); headers.put("testreqheader", "testreqheadervalue"); return builder .given("test state") // NOTE: Using provider states are optional, you can leave it out .uponReceiving("a request for something") .path("/") .method("GET") .headers(headers) .body("{\"test\":true}") .willRespondWith() .status(200) .headers(headers) .body("{\"responsetest\":true}").toFragment(); } @Override protected String providerName() { return "Some Provider"; } @Override protected String consumerName() { return "Some Consumer"; } @Override protected void runTest(String url) { try { assertEquals(new ProviderClient(url).getSomething(), "{\"responsetest\":true}"); } catch (Exception e) { // NOTE: if you want to see any pact failure, do not throw an exception here. This should be // fixed at some point (see Issue #40 throw new RuntimeException(e); } } } ``` ### Using the Pact DSL directly Sometimes it is not convenient to use the ConsumerPactTest as it only allows one test per test class. The DSL can be used directly in this case. Example: ```java import; import; import org.junit.Test; import; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; public class PactTest { @Test public void testPact() { PactFragment pactFragment = ConsumerPactBuilder .consumer("Some Consumer") .hasPactWith("Some Provider") .uponReceiving("a request to say Hello") .path("/hello") .method("POST") .body("{\"name\": \"harry\"}") .willRespondWith() .status(200) .body("{\"hello\": \"harry\"}") .toFragment(); MockProviderConfig config = MockProviderConfig.createDefault(); VerificationResult result = pactFragment.runConsumer(config, new TestRun() { @Override public void run(MockProviderConfig config) { Map expectedResponse = new HashMap(); expectedResponse.put("hello", "harry"); try { assertEquals(new ProviderClient(config.url()).hello("{\"name\": \"harry\"}"), expectedResponse); } catch (IOException e) {} } }); if (result instanceof Pa
Last modified2014-12-02 09:26:13 UTC
Repository URL
Packaging jar
POM File View
Effective POM File View
Dependency tag
<dependency org="" name="pact-jvm-consumer-junit_2.10" rev="2.1.7" />
@Grapes(@Grab(group='', module='pact-jvm-consumer-junit_2.10', version='2.1.7'))
compile ''
libraryDependencies += "" % "pact-jvm-consumer-junit_2.10" % "2.1.7"
Repository tag