Skip to content

Commit c38e6c1

Browse files
committed
initial commit for rest testing with cucumber
1 parent f53ac9d commit c38e6c1

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

‎rest-testing/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@
110110
<scope>test</scope>
111111
</dependency>
112112

113+
<dependency>
114+
<groupId>info.cukes</groupId>
115+
<artifactId>cucumber-java</artifactId>
116+
<version>1.2.4</version>
117+
<scope>test</scope>
118+
</dependency>
119+
<dependency>
120+
<groupId>info.cukes</groupId>
121+
<artifactId>cucumber-junit</artifactId>
122+
<version>1.2.4</version>
123+
</dependency>
113124
</dependencies>
114125

115126
<build>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Testing a REST API
2+
Users should be able to submit GET and POST requests to a web service, represented by WireMock
3+
4+
Scenario: Data Upload to a web service
5+
When users upload data on a project
6+
Then the server should handle it and return a success status
7+
8+
Scenario: Data retrieval from a web service
9+
When users want to get information on the Cucumber project
10+
Then the requested data is returned
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"testing-framework": "cucumber",
3+
"supported-language":
4+
[
5+
"Ruby",
6+
"Java",
7+
"Javascript",
8+
"PHP",
9+
"Python",
10+
"C++"
11+
],
12+
13+
"website": "cucumber.io"
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.rest.cucumber;
2+
3+
import org.junit.runner.RunWith;
4+
import cucumber.api.CucumberOptions;
5+
import cucumber.api.junit.Cucumber;
6+
7+
@RunWith(Cucumber.class)
8+
@CucumberOptions(features = "classpath:Feature")
9+
public class CucumberTest {
10+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.rest.cucumber;
2+
3+
import static org.junit.Assert.*;
4+
import static org.hamcrest.CoreMatchers.containsString;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.Scanner;
10+
11+
import org.apache.http.HttpResponse;
12+
import org.apache.http.client.methods.HttpGet;
13+
import org.apache.http.client.methods.HttpPost;
14+
import org.apache.http.entity.StringEntity;
15+
import org.apache.http.impl.client.CloseableHttpClient;
16+
import org.apache.http.impl.client.HttpClients;
17+
18+
import com.github.tomakehurst.wiremock.WireMockServer;
19+
20+
import cucumber.api.java.en.Then;
21+
import cucumber.api.java.en.When;
22+
23+
public class StepDefinition {
24+
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("cucumber.json");
25+
String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
26+
27+
WireMockServer wireMockServer = new WireMockServer();
28+
CloseableHttpClient httpClient = HttpClients.createDefault();
29+
30+
@When("^users upload data on a project$")
31+
public void usersUploadDataOnAProject() throws IOException {
32+
wireMockServer.start();
33+
34+
configureFor("localhost", 8080);
35+
stubFor(post(urlEqualTo("/create")).withHeader("content-type", equalTo("application/json")).withRequestBody(containing("testing-framework")).willReturn(aResponse().withStatus(200)));
36+
37+
HttpPost request = new HttpPost("http://localhost:8080/create");
38+
StringEntity entity = new StringEntity(jsonString);
39+
request.addHeader("content-type", "application/json");
40+
request.setEntity(entity);
41+
HttpResponse response = httpClient.execute(request);
42+
43+
assertEquals(200, response.getStatusLine().getStatusCode());
44+
verify(postRequestedFor(urlEqualTo("/create")).withHeader("content-type", equalTo("application/json")));
45+
46+
wireMockServer.stop();
47+
}
48+
49+
@When("^users want to get information on the (.+) project$")
50+
public void usersGetInformationOnAProject(String projectName) throws IOException {
51+
wireMockServer.start();
52+
53+
configureFor("localhost", 8080);
54+
stubFor(get(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo("application/json")).willReturn(aResponse().withBody(jsonString)));
55+
56+
HttpGet request = new HttpGet("http://localhost:8080/projects/" + projectName.toLowerCase());
57+
request.addHeader("accept", "application/json");
58+
HttpResponse httpResponse = httpClient.execute(request);
59+
String responseString = convertResponseToString(httpResponse);
60+
61+
assertThat(responseString, containsString("\"testing-framework\": \"cucumber\""));
62+
assertThat(responseString, containsString("\"website\": \"cucumber.io\""));
63+
verify(getRequestedFor(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo("application/json")));
64+
65+
wireMockServer.stop();
66+
}
67+
68+
@Then("^the server should handle it and return a success status$")
69+
public void theServerShouldReturnASuccessStatus() {
70+
}
71+
72+
@Then("^the requested data is returned$")
73+
public void theRequestedDataIsReturned() {
74+
}
75+
76+
private String convertResponseToString(HttpResponse response) throws IOException {
77+
InputStream responseStream = response.getEntity().getContent();
78+
Scanner scanner = new Scanner(responseStream, "UTF-8");
79+
String responseString = scanner.useDelimiter("\\Z").next();
80+
scanner.close();
81+
return responseString;
82+
}
83+
}

0 commit comments

Comments
 (0)