Skip to content

Commit ee1e3ee

Browse files
committed
cucumber parallel features
1 parent 22c9996 commit ee1e3ee

3 files changed

Lines changed: 72 additions & 19 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@
106106
<filtering>true</filtering>
107107
</resource>
108108
</resources>
109+
110+
<plugins>
111+
<plugin>
112+
<artifactId>maven-failsafe-plugin</artifactId>
113+
<version>${maven-failsafe-plugin.version}</version>
114+
<configuration>
115+
<parallel>classes</parallel>
116+
<threadCount>4</threadCount>
117+
</configuration>
118+
<executions>
119+
<execution>
120+
<goals>
121+
<goal>integration-test</goal>
122+
<goal>verify</goal>
123+
</goals>
124+
</execution>
125+
</executions>
126+
</plugin>
127+
<plugin>
128+
<groupId>com.github.temyers</groupId>
129+
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
130+
<version>5.0.0</version>
131+
<executions>
132+
<execution>
133+
<id>generateRunners</id>
134+
<phase>generate-test-sources</phase>
135+
<goals>
136+
<goal>generateRunners</goal>
137+
</goals>
138+
<configuration>
139+
<glue>
140+
<package>com.baeldung.rest.cucumber</package>
141+
</glue>
142+
<featuresDirectory>src/test/resources/Feature/</featuresDirectory>
143+
<parallelScheme>SCENARIO</parallelScheme>
144+
</configuration>
145+
</execution>
146+
</executions>
147+
</plugin>
148+
</plugins>
109149

110150
</build>
111151

‎testing-modules/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package com.baeldung.rest.cucumber;
22

3-
import com.github.tomakehurst.wiremock.WireMockServer;
4-
import cucumber.api.java.en.Then;
5-
import cucumber.api.java.en.When;
6-
import org.apache.http.HttpResponse;
7-
import org.apache.http.client.methods.HttpGet;
8-
import org.apache.http.client.methods.HttpPost;
9-
import org.apache.http.entity.StringEntity;
10-
import org.apache.http.impl.client.CloseableHttpClient;
11-
import org.apache.http.impl.client.HttpClients;
12-
13-
import java.io.IOException;
14-
import java.io.InputStream;
15-
import java.util.Scanner;
16-
173
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
184
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
195
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
@@ -25,10 +11,27 @@
2511
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
2612
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
2713
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
14+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2815
import static org.hamcrest.CoreMatchers.containsString;
2916
import static org.junit.Assert.assertEquals;
3017
import static org.junit.Assert.assertThat;
3118

19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.Scanner;
22+
23+
import org.apache.http.HttpResponse;
24+
import org.apache.http.client.methods.HttpGet;
25+
import org.apache.http.client.methods.HttpPost;
26+
import org.apache.http.entity.StringEntity;
27+
import org.apache.http.impl.client.CloseableHttpClient;
28+
import org.apache.http.impl.client.HttpClients;
29+
30+
import com.github.tomakehurst.wiremock.WireMockServer;
31+
32+
import cucumber.api.java.en.Then;
33+
import cucumber.api.java.en.When;
34+
3235
public class StepDefinition {
3336

3437
private static final String CREATE_PATH = "/create";
@@ -37,20 +40,20 @@ public class StepDefinition {
3740
private final InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("cucumber.json");
3841
private final String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
3942

40-
private final WireMockServer wireMockServer = new WireMockServer();
43+
private final WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
4144
private final CloseableHttpClient httpClient = HttpClients.createDefault();
4245

4346
@When("^users upload data on a project$")
4447
public void usersUploadDataOnAProject() throws IOException {
4548
wireMockServer.start();
4649

47-
configureFor("localhost", 8080);
50+
configureFor("localhost", wireMockServer.port());
4851
stubFor(post(urlEqualTo(CREATE_PATH))
4952
.withHeader("content-type", equalTo(APPLICATION_JSON))
5053
.withRequestBody(containing("testing-framework"))
5154
.willReturn(aResponse().withStatus(200)));
5255

53-
HttpPost request = new HttpPost("http://localhost:8080/create");
56+
HttpPost request = new HttpPost("http://localhost:" + wireMockServer.port() + "/create");
5457
StringEntity entity = new StringEntity(jsonString);
5558
request.addHeader("content-type", APPLICATION_JSON);
5659
request.setEntity(entity);
@@ -67,11 +70,11 @@ public void usersUploadDataOnAProject() throws IOException {
6770
public void usersGetInformationOnAProject(String projectName) throws IOException {
6871
wireMockServer.start();
6972

70-
configureFor("localhost", 8080);
73+
configureFor("localhost", wireMockServer.port());
7174
stubFor(get(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo(APPLICATION_JSON))
7275
.willReturn(aResponse().withBody(jsonString)));
7376

74-
HttpGet request = new HttpGet("http://localhost:8080/projects/" + projectName.toLowerCase());
77+
HttpGet request = new HttpGet("http://localhost:" + wireMockServer.port() + "/projects/" + projectName.toLowerCase());
7578
request.addHeader("accept", APPLICATION_JSON);
7679
HttpResponse httpResponse = httpClient.execute(request);
7780
String responseString = convertResponseToString(httpResponse);
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

0 commit comments

Comments
 (0)