@@ -231,75 +231,81 @@ In `consumer/src/test/java/au/com/dius/pactworkshop/consumer/ProductConsumerPact
231
231
``` java
232
232
@ExtendWith (PactConsumerTestExt . class)
233
233
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 ) {
237
237
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 ) {
256
256
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 ) {
274
274
Product product = new Product ();
275
275
product. setId(" 09" );
276
276
product. setType(" CREDIT_CARD" );
277
277
product. setName(" Gem Visa" );
278
- List<Product > expected = List . of (product, product);
279
-
278
+ List<Product > expected = Arrays . asList (product, product);
279
+
280
280
RestTemplate restTemplate = new RestTemplateBuilder ()
281
- .rootUri(mockServer. getUrl())
282
- .build();
281
+ .rootUri(mockServer. getUrl())
282
+ .build();
283
283
List<Product > products = new ProductService (restTemplate). getAllProducts();
284
-
284
+
285
285
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 ) {
291
291
Product expected = new Product ();
292
292
expected. setId(" 10" );
293
293
expected. setType(" CREDIT_CARD" );
294
294
expected. setName(" 28 Degrees" );
295
-
295
+
296
296
RestTemplate restTemplate = new RestTemplateBuilder ()
297
- .rootUri(mockServer. getUrl())
298
- .build();
297
+ .rootUri(mockServer. getUrl())
298
+ .build();
299
299
Product product = new ProductService (restTemplate). getProduct(" 10" );
300
-
300
+
301
301
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
+ }
303
309
}
304
310
```
305
311
@@ -327,6 +333,8 @@ A pact file should have been generated in *consumer/build/pacts/FrontendApplicat
327
333
328
334
* 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.
329
335
336
+ Move on to [ step 4] ( https://github.com/pact-foundation/pact-workshop-jvm-spring/tree/step4#step-4---verify-the-provider )
337
+
330
338
## Step 4 - Verify the provider
331
339
332
340
We will need to copy the Pact contract file that was produced from the consumer test into the Provider module. This will help us verify that the provider can meet the requirements as set out in the contract.
0 commit comments