|
1 | 1 | package io.reflectoring;
|
2 | 2 |
|
3 | 3 | import au.com.dius.pact.consumer.Pact;
|
4 |
| -import au.com.dius.pact.consumer.PactProviderRuleMk2; |
5 |
| -import au.com.dius.pact.consumer.PactVerification; |
6 | 4 | import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
|
7 | 5 | import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
| 6 | +import au.com.dius.pact.consumer.junit5.PactConsumerTestExt; |
| 7 | +import au.com.dius.pact.consumer.junit5.PactTestFor; |
8 | 8 | import au.com.dius.pact.model.RequestResponsePact;
|
9 |
| -import org.junit.Rule; |
10 |
| -import org.junit.Test; |
11 |
| -import org.junit.runner.RunWith; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
12 | 11 | import org.springframework.beans.factory.annotation.Autowired;
|
13 | 12 | import org.springframework.boot.test.context.SpringBootTest;
|
14 |
| -import org.springframework.test.context.junit4.SpringRunner; |
15 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 14 | +import static org.assertj.core.api.Assertions.*; |
16 | 15 |
|
17 |
| -@RunWith(SpringRunner.class) |
18 |
| -@SpringBootTest(properties = { |
19 |
| - // overriding provider address |
20 |
| - "userservice.ribbon.listOfServers: localhost:8888" |
| 16 | +@ExtendWith(PactConsumerTestExt.class) |
| 17 | +@ExtendWith(SpringExtension.class) |
| 18 | +@PactTestFor(providerName = "userservice", port = "8888") |
| 19 | +@SpringBootTest({ |
| 20 | + // overriding provider address |
| 21 | + "userservice.ribbon.listOfServers: localhost:8888" |
21 | 22 | })
|
22 |
| -public class UserServiceConsumerTest { |
| 23 | +class UserServiceConsumerTest { |
23 | 24 |
|
24 |
| - @Rule |
25 |
| - public PactProviderRuleMk2 stubProvider = new PactProviderRuleMk2("userservice", "localhost", 8888, this); |
| 25 | + @Autowired |
| 26 | + private UserClient userClient; |
26 | 27 |
|
27 |
| - @Autowired |
28 |
| - private UserClient userClient; |
| 28 | + @Pact(state = "provider accepts a new person", provider = "userservice", consumer = "userclient") |
| 29 | + RequestResponsePact createPersonPact(PactDslWithProvider builder) { |
| 30 | + // @formatter:off |
| 31 | + return builder |
| 32 | + .given("provider accepts a new person") |
| 33 | + .uponReceiving("a request to POST a person") |
| 34 | + .path("/user-service/users") |
| 35 | + .method("POST") |
| 36 | + .willRespondWith() |
| 37 | + .status(201) |
| 38 | + .matchHeader("Content-Type", "application/json") |
| 39 | + .body(new PactDslJsonBody() |
| 40 | + .integerType("id", 42)) |
| 41 | + .toPact(); |
| 42 | + // @formatter:on |
| 43 | + } |
29 | 44 |
|
30 |
| - @Pact(state = "provider accepts a new person", provider = "userservice", consumer = "userclient") |
31 |
| - public RequestResponsePact createPersonPact(PactDslWithProvider builder) { |
32 |
| - return builder |
33 |
| - .given("provider accepts a new person") |
34 |
| - .uponReceiving("a request to POST a person") |
35 |
| - .path("/user-service/users") |
36 |
| - .method("POST") |
37 |
| - .willRespondWith() |
38 |
| - .status(201) |
39 |
| - .matchHeader("Content-Type", "application/json") |
40 |
| - .body(new PactDslJsonBody() |
41 |
| - .integerType("id", 42)) |
42 |
| - .toPact(); |
43 |
| - } |
| 45 | + @Pact(state = "person 42 exists", provider = "userservice", consumer = "userclient") |
| 46 | + RequestResponsePact updatePersonPact(PactDslWithProvider builder) { |
| 47 | + // @formatter:off |
| 48 | + return builder |
| 49 | + .given("person 42 exists") |
| 50 | + .uponReceiving("a request to PUT a person") |
| 51 | + .path("/user-service/users/42") |
| 52 | + .method("PUT") |
| 53 | + .willRespondWith() |
| 54 | + .status(200) |
| 55 | + .matchHeader("Content-Type", "application/json") |
| 56 | + .body(new PactDslJsonBody() |
| 57 | + .stringType("firstName", "Zaphod") |
| 58 | + .stringType("lastName", "Beeblebrox")) |
| 59 | + .toPact(); |
| 60 | + // @formatter:on |
| 61 | + } |
44 | 62 |
|
45 |
| - @Pact(state = "person 42 exists", provider = "userservice", consumer = "userclient") |
46 |
| - public RequestResponsePact updatePersonPact(PactDslWithProvider builder) { |
47 |
| - return builder |
48 |
| - .given("person 42 exists") |
49 |
| - .uponReceiving("a request to PUT a person") |
50 |
| - .path("/user-service/users/42") |
51 |
| - .method("PUT") |
52 |
| - .willRespondWith() |
53 |
| - .status(200) |
54 |
| - .matchHeader("Content-Type", "application/json") |
55 |
| - .body(new PactDslJsonBody() |
56 |
| - .stringType("firstName", "Zaphod") |
57 |
| - .stringType("lastName", "Beeblebrox")) |
58 |
| - .toPact(); |
59 |
| - } |
60 | 63 |
|
| 64 | + @Test |
| 65 | + @PactTestFor(pactMethod = "createPersonPact") |
| 66 | + void verifyCreatePersonPact() { |
| 67 | + User user = new User(); |
| 68 | + user.setFirstName("Zaphod"); |
| 69 | + user.setLastName("Beeblebrox"); |
| 70 | + IdObject id = userClient.createUser(user); |
| 71 | + assertThat(id.getId()).isEqualTo(42); |
| 72 | + } |
61 | 73 |
|
62 |
| - @Test |
63 |
| - @PactVerification(fragment = "createPersonPact") |
64 |
| - public void verifyCreatePersonPact() { |
65 |
| - User user = new User(); |
66 |
| - user.setFirstName("Zaphod"); |
67 |
| - user.setLastName("Beeblebrox"); |
68 |
| - IdObject id = userClient.createUser(user); |
69 |
| - assertThat(id.getId()).isEqualTo(42); |
70 |
| - } |
71 |
| - |
72 |
| - @Test |
73 |
| - @PactVerification(fragment = "updatePersonPact") |
74 |
| - public void verifyUpdatePersonPact() { |
75 |
| - User user = new User(); |
76 |
| - user.setFirstName("Zaphod"); |
77 |
| - user.setLastName("Beeblebrox"); |
78 |
| - User updatedUser = userClient.updateUser(42L, user); |
79 |
| - assertThat(updatedUser.getFirstName()).isEqualTo("Zaphod"); |
80 |
| - assertThat(updatedUser.getLastName()).isEqualTo("Beeblebrox"); |
81 |
| - } |
| 74 | + @Test |
| 75 | + @PactTestFor(pactMethod = "updatePersonPact") |
| 76 | + void verifyUpdatePersonPact() { |
| 77 | + User user = new User(); |
| 78 | + user.setFirstName("Zaphod"); |
| 79 | + user.setLastName("Beeblebrox"); |
| 80 | + User updatedUser = userClient.updateUser(42L, user); |
| 81 | + assertThat(updatedUser.getFirstName()).isEqualTo("Zaphod"); |
| 82 | + assertThat(updatedUser.getLastName()).isEqualTo("Beeblebrox"); |
| 83 | + } |
82 | 84 |
|
83 | 85 | }
|
0 commit comments