<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mos_</title>
    <description>The latest articles on DEV Community by mos_ (@kmos).</description>
    <link>https://dev.to/kmos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F40770%2F8306f76a-44ef-40bc-bd14-8ee732336d3d.jpg</url>
      <title>DEV Community: mos_</title>
      <link>https://dev.to/kmos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kmos"/>
    <language>en</language>
    <item>
      <title>Consumer-Driven Contract Testing with Pact and Java - Part II</title>
      <dc:creator>mos_</dc:creator>
      <pubDate>Wed, 17 Jun 2020 12:00:00 +0000</pubDate>
      <link>https://dev.to/ticinoswcraft/consumer-driven-contract-testing-with-pact-and-java-part-ii-1j69</link>
      <guid>https://dev.to/ticinoswcraft/consumer-driven-contract-testing-with-pact-and-java-part-ii-1j69</guid>
      <description>&lt;p&gt;The &lt;a href="https://dev.to/2020/06/03/contract-tests-part1/"&gt;previous post&lt;/a&gt; explains the principles and motivations behind contract testing. Today we take a look to how write consumer-driven contract tests with pact and Java in a SpringBoot application.&lt;/p&gt;

&lt;p&gt;Pact foundation provides junit5 integration for creation and verification of contracts.&lt;/p&gt;

&lt;p&gt;Let’s start!&lt;/p&gt;

&lt;h2&gt;
  
  
  What you need
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://github.com/DiUS/pact-jvm" rel="noopener noreferrer"&gt;Java Pact Testing framework&lt;/a&gt; used in this example is the &lt;code&gt;4.0.0&lt;/code&gt;, based on &lt;code&gt;v3&lt;/code&gt; &lt;a href="https://github.com/pact-foundation/pact-specification/tree/version-3" rel="noopener noreferrer"&gt;specification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Furthermore, you should have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;jdk 1.8 or later&lt;/li&gt;
&lt;li&gt;maven 3.2+&lt;/li&gt;
&lt;li&gt;a bit of testing knowledge in Spring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find all the presented code on &lt;a href="https://github.com/kmos/contract-test-pact-java-example" rel="noopener noreferrer"&gt;github&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fcontractsp2_01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fcontractsp2_01.png" title="example" alt="example" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The proposed example is similar to the previous one seen in part I: we have a &lt;em&gt;provider&lt;/em&gt; service which expose anAPI that given a sentence and a timestamp, it replies an echo response enriched with local timestamp. This API _is consumed_by another service. To summarize:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;endpoint&lt;/em&gt; &lt;code&gt;POST /api/echo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;request body&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1593373353&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sentence"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hello!"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;response body&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"phrase"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hello! sent at: 1593373353 worked at: 1593373360"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Consumer side
&lt;/h2&gt;

&lt;p&gt;We are driven by Consumer, so then we start working on consumer side: we add the pact maven dependency in consumer &lt;code&gt;pom.xml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;au.com.dius&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;pact-jvm-consumer-junit5&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.0.10&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  create a contract
&lt;/h3&gt;

&lt;p&gt;Let’s start creating a junit5 test with &lt;code&gt;PactConsumerTestExt&lt;/code&gt; junit extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;au.com.dius.pact.consumer.junit5.PactConsumerTestExt&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.junit.jupiter.api.extension.ExtendWith&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@ExtendWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactConsumerTestExt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConsumerContractTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in &lt;code&gt;@BeforeEach&lt;/code&gt; method we can assert that the &lt;code&gt;mockServer&lt;/code&gt; which will serve the contracts is correctly up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@BeforeEach&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setUp&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MockServer&lt;/span&gt; &lt;span class="n"&gt;mockServer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockServer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notNullValue&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ok, now we can create a contract. A contract can be defined with a method &lt;em&gt;annotated&lt;/em&gt; with &lt;code&gt;@Pact&lt;/code&gt; that returns a &lt;code&gt;RequestResponsePact&lt;/code&gt; and provides as parameter &lt;code&gt;PactDslWithProvider&lt;/code&gt;. All methods annotated with &lt;code&gt;@Pact&lt;/code&gt; are used toinstrument the &lt;em&gt;mock server&lt;/em&gt; through the &lt;code&gt;PactDslWithProvider&lt;/code&gt; in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Pact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"providerMicroservice"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"consumerMicroservice"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;RequestResponsePact&lt;/span&gt; &lt;span class="nf"&gt;echoRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactDslWithProvider&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a sentence worked at 1593373360"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;uponReceiving&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"an echo request at 1593373353"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;API_ECHO&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* request */&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;echoRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;willRespondWith&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="cm"&gt;/* response */&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;echoResponse&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toPact&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Pact DSL provides a fluent API very similar to Spring &lt;em&gt;mockMvc&lt;/em&gt;: Here we are saying that when the &lt;em&gt;mock server&lt;/em&gt; receives an &lt;em&gt;echoRequest&lt;/em&gt;, it should return &lt;em&gt;200&lt;/em&gt; and an &lt;em&gt;echoResponse&lt;/em&gt;. The &lt;code&gt;given&lt;/code&gt; and the &lt;code&gt;uponReceiving&lt;/code&gt; method, define &lt;a href="https://martinfowler.com/bliki/GivenWhenThen.html" rel="noopener noreferrer"&gt;the specification in bdd approach&lt;/a&gt; and the Pact testing framework, uses the &lt;code&gt;given&lt;/code&gt; part to bring the provider into the correct state before executing the interaction defined in the contract.&lt;/p&gt;




&lt;h4&gt;
  
  
  Matchers: build a response with PactDslJsonBody
&lt;/h4&gt;

&lt;p&gt;in the previous step we created an interaction using two json object(&lt;em&gt;echoRequest&lt;/em&gt; and &lt;em&gt;echoResponse&lt;/em&gt;). On the provider side, the test verify that the generated response is &lt;em&gt;perfectly&lt;/em&gt; equal to the one defined in the contract.&lt;/p&gt;

&lt;p&gt;The Pact testing framework provides also a DSL that permits the definition of different matching case in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Pact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"providerMicroservice"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"consumerMicroservice"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;RequestResponsePact&lt;/span&gt; &lt;span class="nf"&gt;echoRequestWithDsl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactDslWithProvider&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;PactDslJsonBody&lt;/span&gt; &lt;span class="n"&gt;responseWrittenWithDsl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PactDslJsonBody&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stringType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"phrase"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello! sent at: X worked at: Y"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/* match on type */&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asBody&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WITH DSL: a sentence worked at 1593373360"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;uponReceiving&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"an echo request at 1593373353"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;API_ECHO&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;echoRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;willRespondWith&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;responseWrittenWithDsl&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toPact&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we created a &lt;em&gt;response&lt;/em&gt; with &lt;code&gt;PactDslJsonBody&lt;/code&gt; DSL that defines a match case based on &lt;em&gt;type&lt;/em&gt; instead of &lt;em&gt;value&lt;/em&gt;. It’s possible with &lt;code&gt;PactDslJsonBody&lt;/code&gt; different match case based on regex or array length.&lt;/p&gt;




&lt;h3&gt;
  
  
  Verify the contract
&lt;/h3&gt;

&lt;p&gt;Now we can create the real test which verify the contract on the consumer side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="nd"&gt;@PactTestFor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pactMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"echoRequest"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@DisplayName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"given a sentence with a timestamp, when calling producer microservice, than I receive back an echo sentence with a timestamp"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;givenASentenceWithATimestampWhenCallingProducerThanReturnAnEchoWithATimestamp&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MockServer&lt;/span&gt; &lt;span class="n"&gt;mockServer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;BasicHttpEntity&lt;/span&gt; &lt;span class="n"&gt;bodyRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BasicHttpEntity&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;bodyRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setContent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;echoRequest&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Charset&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultCharset&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;

        &lt;span class="n"&gt;expectedResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EchoResponse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;expectedResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPhrase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello! sent at: 1593373353 worked at: 1593373360"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;HttpResponse&lt;/span&gt; &lt;span class="n"&gt;httpResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUrl&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;API_ECHO&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bodyRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;returnResponse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;ObjectMapper&lt;/span&gt; &lt;span class="n"&gt;objectMapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;EchoResponse&lt;/span&gt; &lt;span class="n"&gt;actualResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;objectMapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEntity&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getContent&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;EchoResponse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expectedResult&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;actualResult&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we run &lt;code&gt;mvn test&lt;/code&gt; and we don’t have errors, we will see in &lt;code&gt;./target/pacts&lt;/code&gt; a json file that use the pact formalism for contracts. We use the generated contract in the provider-side.&lt;/p&gt;




&lt;h2&gt;
  
  
  Provider side
&lt;/h2&gt;

&lt;p&gt;For the provider, we have a different dependency to add in &lt;code&gt;pom.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;        &lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;au.com.dius&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;pact-jvm-provider-junit5&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.0.10&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Verify the contract on provider side
&lt;/h3&gt;

&lt;p&gt;Here is the thing: we need to verify the contract against provider implementation. In the Spring world, it’s sounds like an &lt;a href="https://spring.io/guides/gs/testing-web/" rel="noopener noreferrer"&gt;integration test which verify the web layer&lt;/a&gt;. So here the magic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ExtendWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SpringExtension&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@SpringBootTest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webEnvironment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SpringBootTest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WebEnvironment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;DEFINED_PORT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;classes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ProviderApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@EnableAutoConfiguration&lt;/span&gt;
&lt;span class="nd"&gt;@AutoConfigureMockMvc&lt;/span&gt;
&lt;span class="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"classpath:application-contract-test.properties"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Provider&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"providerMicroservice"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@PactFolder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"../consumer/target/pacts"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProviderContractTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${server.host}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;serverHost&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${server.port}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;serverPort&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@BeforeEach&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setupTestTarget&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactVerificationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setTarget&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpTestTarget&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serverHost&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverPort&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@TestTemplate&lt;/span&gt;
    &lt;span class="nd"&gt;@ExtendWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactVerificationInvocationContextProvider&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pactVerificationTestTemplate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactVerificationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;verifyInteraction&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@State&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a sentence worked at 1593373360"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sentenceWorkedAt1593373360&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phraseService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;echo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1593373353&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello!"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;thenReturn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Phrase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello! sent at: 1593373353 worked at: 1593373360"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. As you can see, we have a &lt;code&gt;@SpringBootTest&lt;/code&gt; with a fixed port and a &lt;code&gt;@TestPropertySource&lt;/code&gt; that defines it in order to attach the pact context to the application context with &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; info.Obviously there are other ways, like random ports and so on, but the main thing here is to bind both context together.&lt;/p&gt;

&lt;p&gt;Another thing here is the &lt;code&gt;@PactFolder&lt;/code&gt; annotation that points to contracts generated by the consumer. The Pact Framework search for contracts that belong to the service, and run the verification.&lt;/p&gt;




&lt;h4&gt;
  
  
  The &lt;code&gt;@State&lt;/code&gt; annotation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@State&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a sentence worked at 1593373360"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sentenceWorkedAt1593373360&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phraseService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;echo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1593373353&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello!"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;thenReturn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Phrase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello! sent at: 1593373353 worked at: 1593373360"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As previously mentioned, the &lt;code&gt;given&lt;/code&gt; statement in the consumer contract, define with a business expression, the &lt;code&gt;state&lt;/code&gt; in which the system-under-test, should be during the execution. Following this approach, we define in the provider, a method with &lt;code&gt;@state&lt;/code&gt; annotation, that contains the commands necessary for the correct execution. In our case, we mock the business service delegated to execute the &lt;em&gt;eco logic&lt;/em&gt;. The framework executes the &lt;code&gt;state&lt;/code&gt; method before calling the API defined in the contracts. The real test, in this way, is “reduced” to a simple call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
    &lt;span class="nd"&gt;@TestTemplate&lt;/span&gt;
    &lt;span class="nd"&gt;@ExtendWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactVerificationInvocationContextProvider&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pactVerificationTestTemplate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PactVerificationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;verifyInteraction&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Use a broker
&lt;/h4&gt;

&lt;p&gt;If you have a &lt;code&gt;broker&lt;/code&gt; that stores the contracts, you can change the &lt;code&gt;@PactFolder&lt;/code&gt; annotation with &lt;code&gt;@PactBroker&lt;/code&gt; one and define the following plugin in the &lt;code&gt;pom.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;au.com.dius&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;pact-jvm-provider-maven&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.0.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;pactDirectory&amp;gt;&lt;/span&gt;target/pacts&lt;span class="nt"&gt;&amp;lt;/pactDirectory&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;pactBrokerUrl&amp;gt;&lt;/span&gt;${pact.broker.protocol}://${pact.broker.host}&lt;span class="nt"&gt;&amp;lt;/pactBrokerUrl&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;projectVersion&amp;gt;&lt;/span&gt;${contracts.version}&lt;span class="nt"&gt;&amp;lt;/projectVersion&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;trimSnapshot&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/trimSnapshot&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What’s Next
&lt;/h2&gt;

&lt;p&gt;We have covered how to develop and verify a simple contract with java starting from the consumer. We have used the Pact DSL and matchers introduced in &lt;code&gt;v3&lt;/code&gt; spec which is an interesting feature during design &amp;amp; testing. As previously mentioned, you can find a complete working example in this &lt;a href="https://github.com/kmos/contract-test-pact-java-example" rel="noopener noreferrer"&gt;github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the next posts(I hope), we will see how deploy a broker server to store contracts and how integrate the entire flow witha CI.&lt;/p&gt;

&lt;p&gt;The original post can be found &lt;a href="https://mosfet.io/2020/06/17/contract-test-part2/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>junit5</category>
      <category>testing</category>
      <category>pact</category>
    </item>
    <item>
      <title>Consumer-Driven Contract Testing - Part I</title>
      <dc:creator>mos_</dc:creator>
      <pubDate>Wed, 03 Jun 2020 12:00:00 +0000</pubDate>
      <link>https://dev.to/ticinoswcraft/consumer-driven-contract-testing-part-i-87f</link>
      <guid>https://dev.to/ticinoswcraft/consumer-driven-contract-testing-part-i-87f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the first of a series of blog posts about Contract Testing which cover the minimum set of theory and practice necessary for an effective adoption in your team, from design to code integration.&lt;/p&gt;

&lt;p&gt;Contract Testing is a category of testing activity where the data formats and conventions defined by two systems (services) which communicate a business value, is tested against a Mock called “Contract”. A service &lt;em&gt;provides&lt;/em&gt; a callable API that can be &lt;em&gt;consumed&lt;/em&gt; by another (or many) service which create an interaction between parties that needs to be satisfied during the evolution and developing of services which are now coupled. The interaction between services probably relies on a communication layer which can be slow or not reachable which can affect the test results. For this reason, sometimes the best solution is to verify the interaction with a &lt;a href="https://martinfowler.com/bliki/TestDouble.html" rel="noopener noreferrer"&gt;TestDouble&lt;/a&gt; which describe the expectations between the parties.&lt;/p&gt;

&lt;p&gt;For the sake of clarity, I have &lt;a href="https://www.youtube.com/watch?v=oxbS9Pe2PhE&amp;amp;feature=youtu.be" rel="noopener noreferrer"&gt;strong opinion&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;Let’s suppose that we have a &lt;em&gt;Product&lt;/em&gt; Service which has an HTTP method that provides product information in a JSON format upon receiving a product id:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;HTTP GET: api/v1/product?id=123&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "nexus",
  "type": "smartphone",
  "price": "21.03"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The product information are consumed by &lt;em&gt;Customer&lt;/em&gt; Service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-01.png" title="producer-consumer" alt="producer-consumer" width="524" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conventions are defined in this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Customer&lt;/em&gt; Service is the &lt;em&gt;Consumer&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Product&lt;/em&gt; Service is the &lt;em&gt;Provider&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During developing, the &lt;em&gt;Product&lt;/em&gt; service can evolve the API in a way that the HTTP response change the data format. For example, the &lt;em&gt;type&lt;/em&gt; field from a string value change to object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "nexus",
  "type": {
    "category": "smartphone",
    "weight": 0.2,
    "color": "blue"
  },
  "price": "21.03"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is defined as a &lt;strong&gt;breaking change&lt;/strong&gt; in which &lt;em&gt;Provider&lt;/em&gt; doesn’t respect the defined interaction between &lt;em&gt;Consumer&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the same way, the Customer service can evolve the request call. For example, instead of call the API with a query parameter &lt;code&gt;id&lt;/code&gt;, it can use a field in the header to query the Product Service. Also, this example is a &lt;em&gt;sort of&lt;/em&gt; &lt;strong&gt;breaking change&lt;/strong&gt; in which &lt;em&gt;Consumer&lt;/em&gt; doesn’t respect the defined interaction between &lt;em&gt;Provider&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, the interaction between services can be broken in many ways, and the cause can be triggered by both sides which makes necessary some checks to avoid regression during the development.&lt;/p&gt;

&lt;h2&gt;
  
  
  e2e Testing
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;simplest&lt;/em&gt; thing to do, is to create an &lt;a href="https://martinfowler.com/articles/practical-test-pyramid.html#End-to-endTests" rel="noopener noreferrer"&gt;end-to-end test&lt;/a&gt; that cover the entire calls flow between &lt;em&gt;Consumer Service&lt;/em&gt; and &lt;em&gt;Provider Service&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-02.png" title="e2e" alt="e2e" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Despite what I previously said about &lt;em&gt;simplicity&lt;/em&gt;, e2e testing give the best confidence on software behaviour but with some problems.They are flaky tests and often fail with false positive furthermore e2e tests are hard do maintain, slow, and it’s necessary to span multiple services in testing environment with terrible slowness caused by deploy flow and environment nightmares. There are some solutions (&lt;a href="https://www.soapui.org/" rel="noopener noreferrer"&gt;1&lt;/a&gt;, &lt;a href="https://github.com/postmanlabs/newman" rel="noopener noreferrer"&gt;2&lt;/a&gt;) that can provide the right balance between &lt;em&gt;simplicity&lt;/em&gt; and &lt;em&gt;maintainability&lt;/em&gt; which are a step back e2e tests but often you need a staging environment to execute tests verification or add a level of complexity to remove the environment need, which personally I found with these solutions a tedious process. Moreover, all these solutions are &lt;em&gt;reactive&lt;/em&gt; approaches which in the simplest case, doesn’t avoid the integration of code that breaks the services interactions, but they provide a sort of alerting like open a Jira defect or github issue. It’s possible to avoid code integration in case of breaks but imply an overcomplicated CI or some &lt;a href="https://martinfowler.com/articles/branching-patterns.html#experimental-branch" rel="noopener noreferrer"&gt;experimental branch&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mocks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa903gagztl90xwukpam7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa903gagztl90xwukpam7.gif" alt="Fake news" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Developers&lt;/del&gt; (I) usually dislike maintaining things which depend on environments or that needs hours to have a result. If something is faulty and slow, it’s often untrusted and consequently useless. Furthermore, works with other services means deal with other teams which are often too much busy doing new &lt;del&gt;bugs&lt;/del&gt; features. So then, to avoid this annoying stuff, it’s necessary to adopt a strategy which imply a level of complexity which &lt;em&gt;usually&lt;/em&gt; developers are used to see: &lt;strong&gt;Mocks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Mocks are a type of &lt;em&gt;TestDouble&lt;/em&gt; that define a sort of specification based on expectations and, in this particular case, mocks can substitute APIs or clients reducing in this way, parties to set up and run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-03.png" title="services with mocks" alt="services with mocks" width="800" height="604"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if the image show &lt;em&gt;deployed&lt;/em&gt; services, there are many &lt;a href="https://www.baeldung.com/spring-boot-testing#integration-testing-with-springboottest" rel="noopener noreferrer"&gt;solutions&lt;/a&gt; to load part of an application which doesn’t imply a real execution. however, you can simulate an interaction through a &lt;a href="https://martinfowler.com/articles/mocksArentStubs.html" rel="noopener noreferrer"&gt;mock or stub&lt;/a&gt; based on &lt;strong&gt;Contracts&lt;/strong&gt;. As showed before, we can have two categories of breaking change based on the side that doesn’t maintain expectations and in the same way, we can identify two categories of contracts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provider Contracts&lt;/li&gt;
&lt;li&gt;Consumer Contracts&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Contracts characteristics
&lt;/h3&gt;

&lt;p&gt;A &lt;em&gt;Provider&lt;/em&gt; service exposes a set of business functionalities which can be &lt;strong&gt;used or not&lt;/strong&gt; by one or many consumers and ingested in different ways. A change in the provider interface can break interactions with many consumers, but it isn't the same for the consumer. In the same way, a Provider contract cover completely the functionalities exposed by the service with only one definition. On the contrary, a Consumer contract can cover only a set of functionalities that are interesting for the service.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-04_1.png" title="consumer contracts" alt="consumer contracts" width="800" height="632"&gt;
&lt;/h4&gt;

&lt;p&gt;We can say that a provider contract is a &lt;strong&gt;closed&lt;/strong&gt; and &lt;strong&gt;complete&lt;/strong&gt; expression of business functionalities. Instead, consumer contract is an &lt;strong&gt;open&lt;/strong&gt; and &lt;strong&gt;incomplete&lt;/strong&gt; definition of business expectations. When a provider accepts the expectations defined by the consumer contract, confirms that the expectation is a functionality that supported for a period of time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consumer-driven Contracts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fgooglesearch.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fgooglesearch.jpg" title="google search true story" alt="google search true story" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so now? Well, If you take a look on internet, &lt;a href="https://pactflow.io/blog/the-curious-case-for-the-provider-driven-contract" rel="noopener noreferrer"&gt;&lt;del&gt;you will not find&lt;/del&gt;&lt;/a&gt; any trace of provider driven contract testing &lt;a href="https://github.com/DiUS/pact-jvm/issues/327" rel="noopener noreferrer"&gt;&lt;del&gt;or question about it&lt;/del&gt;&lt;/a&gt;.On the contrary, &lt;a href="https://lmgtfy.com/?q=consumer+driven+contract+testing&amp;amp;s=d" rel="noopener noreferrer"&gt;the web is full of posts about Consumer-driven contract testing&lt;/a&gt;.This should be enough for you and me to choose a consumer-driven solution but, as I said at the beginning, the idea of this post is to give the minimum set of knowledge about Contract testing, and to have an answer in case of your colleagues are free mind enough to ask if exists an alternative meanwhile you are exposing why Contract tests are so &lt;a href="https://reflectoring.io/7-reasons-for-consumer-driven-contracts/" rel="noopener noreferrer"&gt;cool&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In short terms, it’s all about business. Consumer contracts point the finger on current supported business value exposed by the provider. In a consumer-driven approach, the sum of all contracts generated by the consumers and asserted by the provider is &lt;strong&gt;closed&lt;/strong&gt; and &lt;strong&gt;complete&lt;/strong&gt; respect the functionalities requested.&lt;/p&gt;

&lt;p&gt;Anyway Consumer-driven Contracts drive the evolution of the services keeping the focus on what really matter with a feedback loop on the changes that will arrive during the life and death of the services which cannot be achievable with a Provider-driven approach which not take in account the feedback from the consumer.&lt;/p&gt;

&lt;p&gt;In my experience, it’s easier and natural to think in a Provider-First manner than Consumer-first, and the reason behind this mindset, it might be connected to the fact that a breaking change made by the Provider is more detectable than the one made by the Consumer. In a real world example where your team isn’t the owner of both services, you have to deal with meetings, longer meetings and extravagant, informal, hermetic, long-winded design docs and other &lt;a href="https://en.wikipedia.org/wiki/The_Mythical_Man-Month" rel="noopener noreferrer"&gt;mythical beasts&lt;/a&gt;. A contract is a formal expression of needs and duties which can be another tool in your pocket that can be used in order to reduce the background noise during the process of evolution of services. Probably you know the sensation of powerlessness when a design doc or &lt;a href="https://www.openapis.org/" rel="noopener noreferrer"&gt;swagger definition&lt;/a&gt; reaches the mailbox or a shared folder and it’s necessary another meeting or mail tread to have a change in the definition or worse, get an HTTP response status error because someone made a little change in the API which isn’t versioned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-07.png" title="Consumer Driven Process" alt="Consumer Driven Process" width="800" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To summarize, Consumer-Driven contracts can give you a process that presents an iterative way of proceeding with a formalised format which can help large organization with services which are owned by different teams that can be in different locations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contract Testing with Pact
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.pact.io/" rel="noopener noreferrer"&gt;Pact.io&lt;/a&gt; is an implementation of Consumer-driven contract testing which actually support &lt;a href="https://docs.pact.io/implementation_guides/other_languages" rel="noopener noreferrer"&gt;many languages&lt;/a&gt;. At the moment of writing this post, pact foundation released &lt;a href="https://github.com/pact-foundation/pact-specification" rel="noopener noreferrer"&gt;v3 specification&lt;/a&gt; which cover the following features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pact format for message queues&lt;/li&gt;
&lt;li&gt;regular expression and type matching&lt;/li&gt;
&lt;li&gt;specification shared between Ruby, JVM and .Net versions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-05_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mosfet.io%2Fimg%2Fservices-05_1.png" title="Pact ecosystem" alt="Pact ecosystem" width="800" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, it’s available a &lt;a href="https://github.com/pact-foundation/pact_broker" rel="noopener noreferrer"&gt;broker&lt;/a&gt; which can be used to publish and share contracts between services.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s Next
&lt;/h2&gt;

&lt;p&gt;We have covered the minimum set of principles and motivations that are necessary in my opinion to work with contract testing. In the next posts we will take a look to how:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create consumer-driven contracts tests with Pact and Java&lt;/li&gt;
&lt;li&gt;setup broker server&lt;/li&gt;
&lt;li&gt;integration with Continuous Integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The original article can be found &lt;a href="https://mosfet.io/2020/06/03/contract-tests-part1/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/articles/consumerDrivenContracts.html#Schematron" rel="noopener noreferrer"&gt;Consumer-Driven Contracts: A Service Evolution Pattern&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/bliki/ContractTest.html" rel="noopener noreferrer"&gt;ContractTest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/bliki/TestDouble.html" rel="noopener noreferrer"&gt;TestDouble&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pactflow.io/blog/the-curious-case-for-the-provider-driven-contract" rel="noopener noreferrer"&gt;The curious case for the Provider Driven Contract&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/articles/practical-test-pyramid.html" rel="noopener noreferrer"&gt;The Practical Test Pyramid&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/articles/branching-patterns.html" rel="noopener noreferrer"&gt;Patterns for Managing Source COde Branches&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/articles/mocksArentStubs.html" rel="noopener noreferrer"&gt;Mocks Aren’t Stubs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reflectoring.io/7-reasons-for-consumer-driven-contracts/" rel="noopener noreferrer"&gt;7 Reasons to Choose Consumer-Driven Contract Tests Over End-to-End Tests&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>testing</category>
      <category>development</category>
    </item>
  </channel>
</rss>
