Skip to content

Commit 0da5c11

Browse files
committed
BAEL-2492 Scenarios with WireMock
1 parent e2f3d60 commit 0da5c11

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

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

0 commit comments

Comments
 (0)