Skip to content

Commit 4f2d35d

Browse files
author
Ronald Holshausen
committed
chore: update to support JDK 8
1 parent 6da5174 commit 4f2d35d

File tree

2 files changed

+72
-56
lines changed

2 files changed

+72
-56
lines changed

README.md

+61-53
Original file line numberDiff line numberDiff line change
@@ -231,75 +231,81 @@ In `consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPact
231231
```java
232232
@ExtendWith(PactConsumerTestExt.class)
233233
public class ProductConsumerPactTest {
234-
235-
@Pact(consumer = "FrontendApplication", provider = "ProductService")
236-
RequestResponsePact getAllProducts(PactDslWithProvider builder) {
234+
235+
@Pact(consumer = "FrontendApplication", provider = "ProductService")
236+
RequestResponsePact getAllProducts(PactDslWithProvider builder) {
237237
return builder.given("products exist")
238-
.uponReceiving("get all products")
239-
.method("GET")
240-
.path("/products")
241-
.willRespondWith()
242-
.status(200)
243-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
244-
.body(newJsonArrayMinLike(2, array -> {
245-
array.object(object -> {
246-
object.stringType("id", "09");
247-
object.stringType("type", "CREDIT_CARD");
248-
object.stringType("name", "Gem Visa");
249-
});
250-
}).build())
251-
.toPact();
252-
}
253-
254-
@Pact(consumer = "FrontendApplication", provider = "ProductService")
255-
RequestResponsePact getOneProduct(PactDslWithProvider builder) {
238+
.uponReceiving("get all products")
239+
.method("GET")
240+
.path("/products")
241+
.willRespondWith()
242+
.status(200)
243+
.headers(headers())
244+
.body(newJsonArrayMinLike(2, array ->
245+
array.object(object -> {
246+
object.stringType("id", "09");
247+
object.stringType("type", "CREDIT_CARD");
248+
object.stringType("name", "Gem Visa");
249+
})
250+
).build())
251+
.toPact();
252+
}
253+
254+
@Pact(consumer = "FrontendApplication", provider = "ProductService")
255+
RequestResponsePact getOneProduct(PactDslWithProvider builder) {
256256
return builder.given("product with ID 10 exists")
257-
.uponReceiving("get product with ID 10")
258-
.method("GET")
259-
.path("/products/10")
260-
.willRespondWith()
261-
.status(200)
262-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
263-
.body(newJsonBody(object -> {
264-
object.stringType("id", "10");
265-
object.stringType("type", "CREDIT_CARD");
266-
object.stringType("name", "28 Degrees");
267-
}).build())
268-
.toPact();
269-
}
270-
271-
@Test
272-
@PactTestFor(pactMethod = "getAllProducts")
273-
void getAllProducts_whenProductsExist(MockServer mockServer) {
257+
.uponReceiving("get product with ID 10")
258+
.method("GET")
259+
.path("/products/10")
260+
.willRespondWith()
261+
.status(200)
262+
.headers(headers())
263+
.body(newJsonBody(object -> {
264+
object.stringType("id", "10");
265+
object.stringType("type", "CREDIT_CARD");
266+
object.stringType("name", "28 Degrees");
267+
}).build())
268+
.toPact();
269+
}
270+
271+
@Test
272+
@PactTestFor(pactMethod = "getAllProducts")
273+
void getAllProducts_whenProductsExist(MockServer mockServer) {
274274
Product product = new Product();
275275
product.setId("09");
276276
product.setType("CREDIT_CARD");
277277
product.setName("Gem Visa");
278-
List<Product> expected = List.of(product, product);
279-
278+
List<Product> expected = Arrays.asList(product, product);
279+
280280
RestTemplate restTemplate = new RestTemplateBuilder()
281-
.rootUri(mockServer.getUrl())
282-
.build();
281+
.rootUri(mockServer.getUrl())
282+
.build();
283283
List<Product> products = new ProductService(restTemplate).getAllProducts();
284-
284+
285285
assertEquals(expected, products);
286-
}
287-
288-
@Test
289-
@PactTestFor(pactMethod = "getOneProduct")
290-
void getProductById_whenProductWithId10Exists(MockServer mockServer) {
286+
}
287+
288+
@Test
289+
@PactTestFor(pactMethod = "getOneProduct")
290+
void getProductById_whenProductWithId10Exists(MockServer mockServer) {
291291
Product expected = new Product();
292292
expected.setId("10");
293293
expected.setType("CREDIT_CARD");
294294
expected.setName("28 Degrees");
295-
295+
296296
RestTemplate restTemplate = new RestTemplateBuilder()
297-
.rootUri(mockServer.getUrl())
298-
.build();
297+
.rootUri(mockServer.getUrl())
298+
.build();
299299
Product product = new ProductService(restTemplate).getProduct("10");
300-
300+
301301
assertEquals(expected, product);
302-
}
302+
}
303+
304+
private Map<String, String> headers() {
305+
Map<String, String> headers = new HashMap<>();
306+
headers.put("Content-Type", "application/json; charset=utf-8");
307+
return headers;
308+
}
303309
}
304310
```
305311

@@ -326,3 +332,5 @@ Running this test still passes, but it creates a pact file which we can use to v
326332
A pact file should have been generated in *consumer/build/pacts/FrontendApplication-ProductService.json*
327333

328334
*NOTE*: even if the API client had been graciously provided for us by our Provider Team, it doesn't mean that we shouldn't write contract tests - because the version of the client we have may not always be in sync with the deployed API - and also because we will write tests on the output appropriate to our specific needs.
335+
336+
Move on to [step 4](https://github.com/pact-foundation/pact-workshop-jvm-spring/tree/step4#step-4---verify-the-provider)

consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPactTest.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.springframework.boot.web.client.RestTemplateBuilder;
1212
import org.springframework.web.client.RestTemplate;
1313

14+
import java.util.Arrays;
15+
import java.util.HashMap;
1416
import java.util.List;
1517
import java.util.Map;
1618

@@ -29,7 +31,7 @@ RequestResponsePact getAllProducts(PactDslWithProvider builder) {
2931
.path("/products")
3032
.willRespondWith()
3133
.status(200)
32-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
34+
.headers(headers())
3335
.body(newJsonArrayMinLike(2, array ->
3436
array.object(object -> {
3537
object.stringType("id", "09");
@@ -48,7 +50,7 @@ RequestResponsePact getOneProduct(PactDslWithProvider builder) {
4850
.path("/products/10")
4951
.willRespondWith()
5052
.status(200)
51-
.headers(Map.of("Content-Type", "application/json; charset=utf-8"))
53+
.headers(headers())
5254
.body(newJsonBody(object -> {
5355
object.stringType("id", "10");
5456
object.stringType("type", "CREDIT_CARD");
@@ -64,7 +66,7 @@ void getAllProducts_whenProductsExist(MockServer mockServer) {
6466
product.setId("09");
6567
product.setType("CREDIT_CARD");
6668
product.setName("Gem Visa");
67-
List<Product> expected = List.of(product, product);
69+
List<Product> expected = Arrays.asList(product, product);
6870

6971
RestTemplate restTemplate = new RestTemplateBuilder()
7072
.rootUri(mockServer.getUrl())
@@ -89,4 +91,10 @@ void getProductById_whenProductWithId10Exists(MockServer mockServer) {
8991

9092
assertEquals(expected, product);
9193
}
94+
95+
private Map<String, String> headers() {
96+
Map<String, String> headers = new HashMap<>();
97+
headers.put("Content-Type", "application/json; charset=utf-8");
98+
return headers;
99+
}
92100
}

0 commit comments

Comments
 (0)