Skip to content

Commit f0ee172

Browse files
committed
wip: demo use of metadata
1 parent 6be3d0c commit f0ee172

File tree

2 files changed

+10
-35
lines changed

2 files changed

+10
-35
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'org.springframework.boot' version '3.5.3'
33
id 'io.spring.dependency-management' version '1.1.7'
44
id 'java'
5-
id "au.com.dius.pact" version "4.6.17"
5+
id "au.com.dius.pact" version "4.6.15"
66
}
77

88
group = 'com.example'
@@ -24,7 +24,7 @@ configurations {
2424
dependencies {
2525
implementation 'org.springframework.boot:spring-boot-starter-web'
2626
implementation "org.apache.httpcomponents:fluent-hc:4.5.14"
27-
testImplementation 'au.com.dius.pact.consumer:junit5:4.6.17'
27+
testImplementation 'au.com.dius.pact.consumer:junit5:4.6.15'
2828

2929
compileOnly 'org.projectlombok:lombok'
3030
annotationProcessor 'org.projectlombok:lombok'

src/test/java/com/example/products/ProductsPactTest.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
import org.junit.jupiter.api.extension.ExtendWith;
44
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;
66
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;
88
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
9-
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
109
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
1110
import au.com.dius.pact.consumer.junit5.PactTestFor;
1211
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;
1413

1514
import java.io.IOException;
16-
import java.util.List;
1715

1816
import static org.hamcrest.MatcherAssert.assertThat;
1917
import static org.hamcrest.Matchers.is;
@@ -23,54 +21,31 @@
2321
public class ProductsPactTest {
2422

2523
@Pact(consumer="pactflow-example-consumer-java-junit")
26-
public RequestResponsePact getProduct(PactDslWithProvider builder) {
24+
public V4Pact getProduct(PactBuilder builder) {
2725

2826
PactDslJsonBody body = new PactDslJsonBody();
2927
body.stringType("name", "product name");
3028
body.stringType("type", "product series");
3129
body.stringType("id", "5cc989d0-d800-434c-b4bb-b1268499e850");
3230

3331
return builder
32+
.usingLegacyDsl()
33+
.addMetadataValue("matt", "rocks")
3434
.given("a product with ID 10 exists")
3535
.uponReceiving("a request to get a product")
3636
.path("/product/10")
3737
.method("GET")
3838
.willRespondWith()
3939
.status(200)
4040
.body(body)
41-
.toPact();
41+
.toPact(V4Pact.class);
4242
}
4343

44-
@PactTestFor(pactMethod = "getProduct", pactVersion = PactSpecVersion.V3)
44+
@PactTestFor(pactMethod = "getProduct", pactVersion = PactSpecVersion.V4)
4545
@Test
4646
public void testGetProduct(MockServer mockServer) throws IOException {
4747
Product product = new ProductClient().setUrl(mockServer.getUrl()).getProduct("10");
4848

4949
assertThat(product.getId(), is("5cc989d0-d800-434c-b4bb-b1268499e850"));
5050
}
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-
}
7651
}

0 commit comments

Comments
 (0)