Skip to content

Commit 6f71b21

Browse files
authored
Merge pull request eugenp#6352 from pcoates33/BAEL-2492
BAEL-2492 Using WireMock Scenarios
2 parents 86c0187 + 3c4a517 commit 6f71b21

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎testing-modules/rest-testing/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
88
### Relevant Articles:
99
- [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
1010
- [Introduction to WireMock](http://www.baeldung.com/introduction-to-wiremock)
11+
- [Using WireMock Scenarios](http://www.baeldung.com/using-wiremock-scenarios)
1112
- [REST API Testing with Cucumber](http://www.baeldung.com/cucumber-rest-api-testing)
1213
- [Testing a REST API with JBehave](http://www.baeldung.com/jbehave-rest-testing)
1314
- [REST API Testing with Karate](http://www.baeldung.com/karate-rest-api-testing)

‎testing-modules/rest-testing/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
<!-- testing -->
160160
<rest-assured.version>2.9.0</rest-assured.version>
161161
<cucumber.version>1.2.5</cucumber.version>
162-
<wiremock.version>2.4.1</wiremock.version>
162+
<wiremock.version>2.21.0</wiremock.version>
163163
<karate.version>0.6.1</karate.version>
164164

165165
<httpcore.version>4.4.5</httpcore.version>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.rest.wiremock.scenario;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStreamReader;
12+
13+
import org.apache.http.HttpResponse;
14+
import org.apache.http.client.ClientProtocolException;
15+
import org.apache.http.client.methods.HttpGet;
16+
import org.apache.http.impl.client.CloseableHttpClient;
17+
import org.apache.http.impl.client.HttpClients;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
22+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
23+
24+
public class WireMockScenarioExampleIntegrationTest {
25+
26+
private static final String THIRD_STATE = "third";
27+
private static final String SECOND_STATE = "second";
28+
private static final String TIP_01 = "finally block is not called when System.exit() is called in the try block";
29+
private static final String TIP_02 = "keep your code clean";
30+
private static final String TIP_03 = "use composition rather than inheritance";
31+
private static final String TEXT_PLAIN = "text/plain";
32+
33+
static int port = 9999;
34+
35+
@Rule
36+
public WireMockRule wireMockRule = new WireMockRule(port);
37+
38+
@Test
39+
public void changeStateOnEachCallTest() throws IOException {
40+
createWireMockStub(Scenario.STARTED, SECOND_STATE, TIP_01);
41+
createWireMockStub(SECOND_STATE, THIRD_STATE, TIP_02);
42+
createWireMockStub(THIRD_STATE, Scenario.STARTED, TIP_03);
43+
44+
assertEquals(TIP_01, nextTip());
45+
assertEquals(TIP_02, nextTip());
46+
assertEquals(TIP_03, nextTip());
47+
assertEquals(TIP_01, nextTip());
48+
}
49+
50+
private void createWireMockStub(String currentState, String nextState, String responseBody) {
51+
stubFor(get(urlEqualTo("/java-tip"))
52+
.inScenario("java tips")
53+
.whenScenarioStateIs(currentState)
54+
.willReturn(aResponse()
55+
.withStatus(200)
56+
.withHeader("Content-Type", TEXT_PLAIN)
57+
.withBody(responseBody))
58+
.willSetStateTo(nextState)
59+
);
60+
}
61+
62+
private String nextTip() throws ClientProtocolException, IOException {
63+
CloseableHttpClient httpClient = HttpClients.createDefault();
64+
HttpGet request = new HttpGet(String.format("http://localhost:%s/java-tip", port));
65+
HttpResponse httpResponse = httpClient.execute(request);
66+
return firstLineOfResponse(httpResponse);
67+
}
68+
69+
private static String firstLineOfResponse(HttpResponse httpResponse) throws IOException {
70+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()))) {
71+
return reader.readLine();
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)