Skip to content

Commit d66d02c

Browse files
authored
Merge pull request eugenp#2165 from tomekl007/BAEL-981_pact
BAEL-981 pact article code
2 parents 7ec7a96 + cc55418 commit d66d02c

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@
350350
<artifactId>java-lsh</artifactId>
351351
<version>${java-lsh.version}</version>
352352
</dependency>
353+
<dependency>
354+
<groupId>au.com.dius</groupId>
355+
<artifactId>pact-jvm-consumer-junit_2.11</artifactId>
356+
<version>${pact.version}</version>
357+
<scope>test</scope>
358+
</dependency>
359+
<dependency>
360+
<groupId>org.codehaus.groovy</groupId>
361+
<artifactId>groovy-all</artifactId>
362+
<version>2.4.10</version>
363+
</dependency>
353364
</dependencies>
354365
<properties>
355366
<multiverse.version>0.7.0</multiverse.version>
@@ -379,6 +390,7 @@
379390
<commons.collections.version>4.1</commons.collections.version>
380391
<junit.version>4.12</junit.version>
381392
<java-lsh.version>0.10</java-lsh.version>
393+
<pact.version>3.5.0</pact.version>
382394
</properties>
383395

384396
</project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.baeldung.pact;
2+
3+
4+
import au.com.dius.pact.consumer.Pact;
5+
import au.com.dius.pact.consumer.PactProviderRuleMk2;
6+
import au.com.dius.pact.consumer.PactVerification;
7+
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
8+
import au.com.dius.pact.model.RequestResponsePact;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.springframework.http.*;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
public class PactConsumerDrivenContractUnitTest {
20+
21+
@Rule
22+
public PactProviderRuleMk2 mockProvider
23+
= new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
24+
25+
@Pact(consumer = "test_consumer")
26+
public RequestResponsePact createPact(PactDslWithProvider builder) {
27+
Map<String, String> headers = new HashMap<String, String>();
28+
headers.put("Content-Type", "application/json");
29+
30+
return builder
31+
.given("test GET ")
32+
.uponReceiving("GET REQUEST")
33+
.path("/")
34+
.method("GET")
35+
.willRespondWith()
36+
.status(200)
37+
.headers(headers)
38+
.body("{\"condition\": true, \"name\": \"tom\"}")
39+
.given("test POST")
40+
.uponReceiving("POST REQUEST")
41+
.method("POST")
42+
.headers(headers)
43+
.body("{\"name\": \"Michael\"}")
44+
.path("/create")
45+
.willRespondWith()
46+
.status(201)
47+
.headers(headers)
48+
.body("")
49+
.toPact();
50+
}
51+
52+
53+
@Test
54+
@PactVerification()
55+
public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
56+
//when
57+
ResponseEntity<String> response
58+
= new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
59+
60+
//then
61+
assertThat(response.getStatusCode().value()).isEqualTo(200);
62+
assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue();
63+
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
64+
65+
//and
66+
HttpHeaders httpHeaders = new HttpHeaders();
67+
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
68+
String jsonBody = "{\"name\": \"Michael\"}";
69+
70+
//when
71+
ResponseEntity<String> postResponse = new RestTemplate().exchange(
72+
mockProvider.getUrl() + "/create",
73+
HttpMethod.POST,
74+
new HttpEntity<>(jsonBody, httpHeaders),
75+
String.class
76+
);
77+
78+
//then
79+
assertThat(postResponse.getStatusCode().value()).isEqualTo(201);
80+
}
81+
82+
}

0 commit comments

Comments
 (0)