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.PactTestExecutionContext;
|
||||
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.junit5.PactConsumerTestExt;
|
||||
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
||||
|
@ -27,19 +29,7 @@ public class PactConsumerJUnit5Test {
|
|||
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
||||
public V4Pact getAllUsers(PactBuilder builder) {
|
||||
|
||||
String expectedUsers = """
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Jane Doe"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "John Doe"
|
||||
}
|
||||
]
|
||||
""";
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return builder
|
||||
.usingLegacyDsl()
|
||||
.given("A running user service")
|
||||
|
@ -49,7 +39,16 @@ public class PactConsumerJUnit5Test {
|
|||
.willRespondWith()
|
||||
.status(200)
|
||||
.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()
|
||||
.asV4Pact()
|
||||
.get();
|
||||
|
@ -58,13 +57,6 @@ public class PactConsumerJUnit5Test {
|
|||
@Pact(provider = "UserServiceJUnit5", consumer = "UserConsumer")
|
||||
public V4Pact getUserById(PactBuilder builder) {
|
||||
|
||||
String expectedUser = """
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Jane Doe"
|
||||
}
|
||||
""";
|
||||
|
||||
return builder
|
||||
.usingLegacyDsl()
|
||||
.given("A running user service")
|
||||
|
@ -74,7 +66,10 @@ public class PactConsumerJUnit5Test {
|
|||
.willRespondWith()
|
||||
.status(200)
|
||||
.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()
|
||||
.asV4Pact()
|
||||
.get();
|
||||
|
@ -86,23 +81,25 @@ public class PactConsumerJUnit5Test {
|
|||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build();
|
||||
User user = restTemplate.getForObject("/users/1", User.class);
|
||||
|
||||
assertThat(user)
|
||||
.isNotNull()
|
||||
.isEqualTo(new User(1, "Jane Doe"));
|
||||
assertThat(user).isNotNull();
|
||||
assertThat(user.id()).isNotNegative();
|
||||
assertThat(user.name()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@PactTestFor(providerName = "UserServiceJUnit5", pactMethod = "getAllUsers")
|
||||
void getAllUsers(MockServer mockServer) {
|
||||
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()
|
||||
.containsExactly(
|
||||
new User(1, "Jane Doe"),
|
||||
new User(2, "John Doe")
|
||||
);
|
||||
.hasSize(2);
|
||||
|
||||
users.forEach(user -> {
|
||||
assertThat(user.id()).isNotNegative();
|
||||
assertThat(user.name()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue