Merge pull request #6352 from pcoates33/BAEL-2492
BAEL-2492 Using WireMock Scenarios
This commit is contained in:
commit
6f71b21996
|
@ -8,6 +8,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
### Relevant Articles:
|
||||
- [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
- [Introduction to WireMock](http://www.baeldung.com/introduction-to-wiremock)
|
||||
- [Using WireMock Scenarios](http://www.baeldung.com/using-wiremock-scenarios)
|
||||
- [REST API Testing with Cucumber](http://www.baeldung.com/cucumber-rest-api-testing)
|
||||
- [Testing a REST API with JBehave](http://www.baeldung.com/jbehave-rest-testing)
|
||||
- [REST API Testing with Karate](http://www.baeldung.com/karate-rest-api-testing)
|
||||
|
|
|
@ -159,7 +159,7 @@
|
|||
<!-- testing -->
|
||||
<rest-assured.version>2.9.0</rest-assured.version>
|
||||
<cucumber.version>1.2.5</cucumber.version>
|
||||
<wiremock.version>2.4.1</wiremock.version>
|
||||
<wiremock.version>2.21.0</wiremock.version>
|
||||
<karate.version>0.6.1</karate.version>
|
||||
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.rest.wiremock.scenario;
|
||||
|
||||
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.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||
import com.github.tomakehurst.wiremock.stubbing.Scenario;
|
||||
|
||||
public class WireMockScenarioExampleIntegrationTest {
|
||||
|
||||
private static final String THIRD_STATE = "third";
|
||||
private static final String SECOND_STATE = "second";
|
||||
private static final String TIP_01 = "finally block is not called when System.exit() is called in the try block";
|
||||
private static final String TIP_02 = "keep your code clean";
|
||||
private static final String TIP_03 = "use composition rather than inheritance";
|
||||
private static final String TEXT_PLAIN = "text/plain";
|
||||
|
||||
static int port = 9999;
|
||||
|
||||
@Rule
|
||||
public WireMockRule wireMockRule = new WireMockRule(port);
|
||||
|
||||
@Test
|
||||
public void changeStateOnEachCallTest() throws IOException {
|
||||
createWireMockStub(Scenario.STARTED, SECOND_STATE, TIP_01);
|
||||
createWireMockStub(SECOND_STATE, THIRD_STATE, TIP_02);
|
||||
createWireMockStub(THIRD_STATE, Scenario.STARTED, TIP_03);
|
||||
|
||||
assertEquals(TIP_01, nextTip());
|
||||
assertEquals(TIP_02, nextTip());
|
||||
assertEquals(TIP_03, nextTip());
|
||||
assertEquals(TIP_01, nextTip());
|
||||
}
|
||||
|
||||
private void createWireMockStub(String currentState, String nextState, String responseBody) {
|
||||
stubFor(get(urlEqualTo("/java-tip"))
|
||||
.inScenario("java tips")
|
||||
.whenScenarioStateIs(currentState)
|
||||
.willReturn(aResponse()
|
||||
.withStatus(200)
|
||||
.withHeader("Content-Type", TEXT_PLAIN)
|
||||
.withBody(responseBody))
|
||||
.willSetStateTo(nextState)
|
||||
);
|
||||
}
|
||||
|
||||
private String nextTip() throws ClientProtocolException, IOException {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet request = new HttpGet(String.format("http://localhost:%s/java-tip", port));
|
||||
HttpResponse httpResponse = httpClient.execute(request);
|
||||
return firstLineOfResponse(httpResponse);
|
||||
}
|
||||
|
||||
private static String firstLineOfResponse(HttpResponse httpResponse) throws IOException {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()))) {
|
||||
return reader.readLine();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue