au.com.dius:pact-jvm-consumer-junit_2.11:2.2.1

Group / Artifact / Version au.com.dius / pact-jvm-consumer-junit_2.11 / 2.2.1
Namepact-jvm-consumer-junit_2.11
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 = `au.com.dius` * 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 au.com.dius.pact.model.PactFragment; 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 https://github.com/DiUS/pact-jvm/issues/40) throw new RuntimeException(e); } } } ``` ### Using the Pact JUnit Rule Thanks to [@warmuuh](https://github.com/warmuuh) we have a JUnit rule that simplifies running Pact consumer tests. To use it, create a test class and then add the rule: #### 1. Add the Pact Rule to your test class. ```java @Rule public PactRule rule = new PactRule("localhost", 8080, this); ``` #### 2. Annotate a method with Pact that returns a pact fragment ```java @Pact(state="test state", provider="test_provider", consumer="test_consumer") public PactFragment createFragment(PactDslWithState builder) { return builder .uponReceiving("ExampleJavaConsumerPactRuleTest test interaction") .path("/") .method("GET") .willRespondWith() .status(200) .body("{\"responsetest\": true}") .toFragment(); } ``` #### 3. Annotate your test method with PactVerification to have it run in the context of a mock server setup with the appropriate pact from step 2 ```java @Test @PactVerification("test state") public void runTest() { Map expectedResponse = new HashMap(); expectedResponse.put("responsetest", true); assertEquals(new ConsumerClient("http://localhost:8080").get("/"), expectedResponse); } ``` For an example, have a look at [ExampleJavaConsumerPactRuleTest](src/test/java/au/com/dius/pact/consumer/examples/ExampleJavaConsumerPactRuleTest.java) ### Using the Pact DSL directly Sometimes it is not convenient to use the ConsumerPactTest as it only al
Last modified2015-05-21 10:18:45 UTC
URL https://github.com/DiUS/pact-jvm
Repository URL http://repo1.maven.org/maven2/au/com/dius/pact-jvm-consumer-junit_2.11/2.2.1/
Packaging jar
POM File View
Effective POM File View
Dependency tag
<dependency>
  <groupId>au.com.dius</groupId>
  <artifactId>pact-jvm-consumer-junit_2.11</artifactId>
  <version>2.2.1</version>
</dependency>
'au.com.dius:pact-jvm-consumer-junit_2.11:jar:2.2.1'
<dependency org="au.com.dius" name="pact-jvm-consumer-junit_2.11" rev="2.2.1" />
@Grapes(@Grab(group='au.com.dius', module='pact-jvm-consumer-junit_2.11', version='2.2.1'))
compile 'au.com.dius:pact-jvm-consumer-junit_2.11:2.2.1'
libraryDependencies += "au.com.dius" % "pact-jvm-consumer-junit_2.11" % "2.2.1"
Repository tag
  <repository>
    <id>central</id>
    <url>http://repo1.maven.org/maven2/</url>
  </repository>
  <repository>
    <id>ibiblio.org-releases</id>
    <url>http://mirrors.ibiblio.org/maven2/</url>
  </repository>
  <repository>
    <id>sonatype-releases</id>
    <url>https://oss.sonatype.org/content/repositories/releases/</url>
  </repository>
  <repository>
    <id>sonatype-staging</id>
    <url>https://oss.sonatype.org/content/repositories/staging/</url>
  </repository>
  <repository>
    <id>spring-milestones</id>
    <url>http://repo.spring.io/libs-milestone/</url>
  </repository>
  <repository>
    <id>spring-plugin-releases</id>
    <url>http://repo.spring.io/plugins-release/</url>
  </repository>
  <repository>
    <id>spring-releases</id>
    <url>http://repo.spring.io/libs-release-remote/</url>
  </repository>
Packages

au.com.dius.pact.consumer