Change pact to use value matching
This commit is contained in:
parent
ec9c105e3b
commit
c0dcb9fc16
1 changed files with 28 additions and 31 deletions
|
@ -3,6 +3,8 @@ package de.rpr.pactexample;
|
||||||
import au.com.dius.pact.consumer.MockServer;
|
import au.com.dius.pact.consumer.MockServer;
|
||||||
import au.com.dius.pact.consumer.PactTestExecutionContext;
|
import au.com.dius.pact.consumer.PactTestExecutionContext;
|
||||||
import au.com.dius.pact.consumer.dsl.PactBuilder;
|
import au.com.dius.pact.consumer.dsl.PactBuilder;
|
||||||
|
import au.com.dius.pact.consumer.dsl.PactDslJsonArray;
|
||||||
|
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
|
||||||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
||||||
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
|
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
|
||||||
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
||||||
|
@ -27,19 +29,7 @@ public class PactConsumerJUnit5Test {
|
||||||
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
||||||
public V4Pact getAllUsers(PactBuilder builder) {
|
public V4Pact getAllUsers(PactBuilder builder) {
|
||||||
|
|
||||||
String expectedUsers = """
|
//noinspection ConstantConditions
|
||||||
[
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"name": "Jane Doe"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"name": "John Doe"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
""";
|
|
||||||
|
|
||||||
return builder
|
return builder
|
||||||
.usingLegacyDsl()
|
.usingLegacyDsl()
|
||||||
.given("A running user service")
|
.given("A running user service")
|
||||||
|
@ -49,7 +39,16 @@ public class PactConsumerJUnit5Test {
|
||||||
.willRespondWith()
|
.willRespondWith()
|
||||||
.status(200)
|
.status(200)
|
||||||
.headers(Map.of("Content-Type", "application/json"))
|
.headers(Map.of("Content-Type", "application/json"))
|
||||||
.body(expectedUsers)
|
.body(new PactDslJsonArray()
|
||||||
|
.object()
|
||||||
|
.integerMatching("id", "[0-9]*", 1)
|
||||||
|
.stringMatcher("name", "[a-zA-Z ]*", "Jane Doe")
|
||||||
|
.closeObject()
|
||||||
|
.object()
|
||||||
|
.integerMatching("id", "[0-9]*", 2)
|
||||||
|
.stringMatcher("name", "[a-zA-Z ]*", "John Doe")
|
||||||
|
.closeObject()
|
||||||
|
)
|
||||||
.toPact()
|
.toPact()
|
||||||
.asV4Pact()
|
.asV4Pact()
|
||||||
.get();
|
.get();
|
||||||
|
@ -58,13 +57,6 @@ public class PactConsumerJUnit5Test {
|
||||||
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
||||||
public V4Pact getUserById(PactBuilder builder) {
|
public V4Pact getUserById(PactBuilder builder) {
|
||||||
|
|
||||||
String expectedUser = """
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"name": "Jane Doe"
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
|
|
||||||
return builder
|
return builder
|
||||||
.usingLegacyDsl()
|
.usingLegacyDsl()
|
||||||
.given("A running user service")
|
.given("A running user service")
|
||||||
|
@ -74,7 +66,10 @@ public class PactConsumerJUnit5Test {
|
||||||
.willRespondWith()
|
.willRespondWith()
|
||||||
.status(200)
|
.status(200)
|
||||||
.headers(Map.of("Content-Type", "application/json"))
|
.headers(Map.of("Content-Type", "application/json"))
|
||||||
.body(expectedUser)
|
.body(new PactDslJsonBody()
|
||||||
|
.integerMatching("id", "[0-9]*", 1)
|
||||||
|
.stringMatcher("name", "[a-zA-Z ]*", "Jane Doe")
|
||||||
|
)
|
||||||
.toPact()
|
.toPact()
|
||||||
.asV4Pact()
|
.asV4Pact()
|
||||||
.get();
|
.get();
|
||||||
|
@ -86,23 +81,25 @@ public class PactConsumerJUnit5Test {
|
||||||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
|
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
|
||||||
User user = restTemplate.getForObject("/users/1", User.class);
|
User user = restTemplate.getForObject("/users/1", User.class);
|
||||||
|
|
||||||
assertThat(user)
|
assertThat(user).isNotNull();
|
||||||
.isNotNull()
|
assertThat(user.id()).isNotNegative();
|
||||||
.isEqualTo(new User(1, "Jane Doe"));
|
assertThat(user.name()).isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@PactTestFor(providerName = "UserServiceJUnit5", pactMethod = "getAllUsers")
|
@PactTestFor(providerName = "UserServiceJUnit5", pactMethod = "getAllUsers")
|
||||||
void getAllUsers(MockServer mockServer) {
|
void getAllUsers(MockServer mockServer) {
|
||||||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
|
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
|
||||||
User[] response = restTemplate.getForObject("/users", User[].class);
|
List<User> users = Arrays.asList(restTemplate.getForObject("/users", User[].class));
|
||||||
|
|
||||||
assertThat(response)
|
assertThat(users)
|
||||||
.isNotNull()
|
.isNotNull()
|
||||||
.containsExactly(
|
.hasSize(2);
|
||||||
new User(1, "Jane Doe"),
|
|
||||||
new User(2, "John Doe")
|
users.forEach(user -> {
|
||||||
);
|
assertThat(user.id()).isNotNegative();
|
||||||
|
assertThat(user.name()).isNotEmpty();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue