2
2
3
3
import org .junit .jupiter .api .extension .ExtendWith ;
4
4
import au .com .dius .pact .consumer .MockServer ;
5
- import au .com .dius .pact .core .model .RequestResponsePact ;
5
+ import au .com .dius .pact .core .model .V4Pact ;
6
6
import au .com .dius .pact .core .model .annotations .Pact ;
7
- import au .com .dius .pact .consumer .dsl .PactDslJsonArray ;
7
+ import au .com .dius .pact .consumer .dsl .PactBuilder ;
8
8
import au .com .dius .pact .consumer .dsl .PactDslJsonBody ;
9
- import au .com .dius .pact .consumer .dsl .PactDslWithProvider ;
10
9
import au .com .dius .pact .consumer .junit5 .PactConsumerTestExt ;
11
10
import au .com .dius .pact .consumer .junit5 .PactTestFor ;
12
11
import org .junit .jupiter .api .Test ;
13
- import au .com .dius .pact .core .model .PactSpecVersion ; // required for v4.6.x to set pactVersion
12
+ import au .com .dius .pact .core .model .PactSpecVersion ;
14
13
15
14
import java .io .IOException ;
16
- import java .util .List ;
17
15
18
16
import static org .hamcrest .MatcherAssert .assertThat ;
19
17
import static org .hamcrest .Matchers .is ;
23
21
public class ProductsPactTest {
24
22
25
23
@ Pact (consumer ="pactflow-example-consumer-java-junit" )
26
- public RequestResponsePact getProduct (PactDslWithProvider builder ) {
24
+ public V4Pact getProduct (PactBuilder builder ) {
27
25
28
26
PactDslJsonBody body = new PactDslJsonBody ();
29
27
body .stringType ("name" , "product name" );
30
28
body .stringType ("type" , "product series" );
31
29
body .stringType ("id" , "5cc989d0-d800-434c-b4bb-b1268499e850" );
32
30
33
31
return builder
32
+ .usingLegacyDsl ()
33
+ .addMetadataValue ("matt" , "rocks" )
34
34
.given ("a product with ID 10 exists" )
35
35
.uponReceiving ("a request to get a product" )
36
36
.path ("/product/10" )
37
37
.method ("GET" )
38
38
.willRespondWith ()
39
39
.status (200 )
40
40
.body (body )
41
- .toPact ();
41
+ .toPact (V4Pact . class );
42
42
}
43
43
44
- @ PactTestFor (pactMethod = "getProduct" , pactVersion = PactSpecVersion .V3 )
44
+ @ PactTestFor (pactMethod = "getProduct" , pactVersion = PactSpecVersion .V4 )
45
45
@ Test
46
46
public void testGetProduct (MockServer mockServer ) throws IOException {
47
47
Product product = new ProductClient ().setUrl (mockServer .getUrl ()).getProduct ("10" );
48
48
49
49
assertThat (product .getId (), is ("5cc989d0-d800-434c-b4bb-b1268499e850" ));
50
50
}
51
-
52
- @ Pact (consumer ="pactflow-example-consumer-java-junit" )
53
- public RequestResponsePact getProducts (PactDslWithProvider builder ) {
54
- return builder
55
- .given ("a product with ID 10 exists" )
56
- .uponReceiving ("a request to get all products" )
57
- .path ("/products" )
58
- .method ("GET" )
59
- .willRespondWith ()
60
- .status (200 )
61
- .body (PactDslJsonArray .arrayEachLike ()
62
- .stringType ("name" , "product name" )
63
- .stringType ("type" , "product series" )
64
- .stringType ("id" , "5cc989d0-d800-434c-b4bb-b1268499e850" )
65
- .closeObject ())
66
- .toPact ();
67
- }
68
-
69
- @ PactTestFor (pactMethod = "getProducts" , pactVersion = PactSpecVersion .V3 )
70
- @ Test
71
- public void testGetProducts (MockServer mockServer ) throws IOException {
72
- List <Product > products = new ProductClient ().setUrl (mockServer .getUrl ()).getProducts ();
73
-
74
- assertThat (products .get (0 ).getId (), is ("5cc989d0-d800-434c-b4bb-b1268499e850" ));
75
- }
76
51
}
0 commit comments