-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExampleTest.java
141 lines (112 loc) · 4.13 KB
/
ExampleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.github.jenspiegsa.wiremockextension;
import static com.github.jenspiegsa.wiremockextension.ManagedWireMockServer.with;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.core.Options;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* @author Jens Piegsa
*/
@DisplayName("WireMockExtension usage")
@ExtendWith(WireMockExtension.class)
class ExampleTest {
@Nested
@DisplayName("1. Simple example")
class SimpleExample {
SampleClient sampleClient;
@BeforeEach
void setup() {
sampleClient = new SampleClient("http://localhost:8080/");
}
@AfterEach
void afterEach() {
sampleClient.close();
}
@Nested
@DisplayName("isOk()")
class IsOk {
@Test
@DisplayName("should return true when status ok (200).")
void shouldReturnTrueWhenStatusOk() {
givenThat(get("/").willReturn(ok()));
// when
final boolean clientOk = sampleClient.isOk();
then(clientOk).isTrue();
}
@Test
@DisplayName("should return false when status not found (404).")
void shouldReturnFalseWhenStatusNotFound() {
givenThat(get("/").willReturn(notFound()));
// when
final boolean clientOk = sampleClient.isOk();
then(clientOk).isFalse();
}
}
}
@Nested
@DisplayName("2. Customized example")
@WireMockSettings(failOnUnmatchedRequests = true)
class CustomExample {
@InjectServer
WireMockServer serverMock;
@ConfigureWireMock
Options options = wireMockConfig()
.dynamicPort()
.notifier(new ConsoleNotifier(true));
SampleClient sampleClient;
@BeforeEach
void setup() {
sampleClient = new SampleClient("http://localhost:" + serverMock.port() + "/");
}
@ParameterizedTest(name = "should return {1} when status {0}")
@DisplayName("isOk()")
@CsvSource({"200, true", "404, false", "503, false"})
void shouldReturnStatusOk(final int status, final boolean expectedOutcome) {
givenThat(get("/").willReturn(aResponse().withStatus(status)));
// when
final boolean actualOutcome = sampleClient.isOk();
then(actualOutcome).isEqualTo(expectedOutcome);
}
@AfterEach
void afterEach() {
sampleClient.close();
}
}
@Nested
@DisplayName("3. Example with multiple servers")
class ExampleWithMultipleServers {
@Managed WireMockServer s1 = with(wireMockConfig().dynamicPort());
@Managed WireMockServer s2 = with(wireMockConfig().dynamicPort());
@Managed WireMockServer s3 = with(wireMockConfig().dynamicPort());
@Managed WireMockServer s4 = with(wireMockConfig().dynamicPort()).failOnUnmatchedRequest(false);
@Test
@DisplayName("should work.")
void shouldWork() {
s1.stubFor(get("/a").willReturn(ok()));
s2.stubFor(get("/b").willReturn(notFound()));
s3.stubFor(get("/c").willReturn(ok()));
then(new SampleClient("http://localhost:" + s1.port() + "/a").isOk()).isTrue();
then(new SampleClient("http://localhost:" + s2.port() + "/b").isOk()).isFalse();
then(new SampleClient("http://localhost:" + s3.port() + "/c").isOk()).isTrue();
then(new SampleClient("http://localhost:" + s4.port() + "/d").isOk()).isFalse();
}
}
}