Skip to content

Commit 66a8aeb

Browse files
committed
2 parents 1137b62 + 49078a0 commit 66a8aeb

File tree

28 files changed

+677
-55
lines changed

28 files changed

+677
-55
lines changed

pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<jbehave.version>4.8.3</jbehave.version>
8686
<ant.version>1.10.12</ant.version>
8787
<restassured.version>5.5.0</restassured.version>
88-
<wiremock.version>1.58</wiremock.version>
88+
<wiremock.version>3.13.0</wiremock.version>
8989
<thymeleaf.version>3.1.2.RELEASE</thymeleaf.version>
9090
<javassist.version>3.29.2-GA</javassist.version>
9191
<jackson.version>2.18.1</jackson.version>
@@ -406,10 +406,28 @@
406406
<version>${jimfs.version}</version>
407407
</dependency>
408408
<dependency>
409-
<groupId>com.github.tomakehurst</groupId>
409+
<groupId>org.wiremock</groupId>
410410
<artifactId>wiremock</artifactId>
411411
<version>${wiremock.version}</version>
412412
<scope>test</scope>
413+
<exclusions>
414+
<exclusion>
415+
<groupId>com.google.guava</groupId>
416+
<artifactId>guava</artifactId>
417+
</exclusion>
418+
<exclusion>
419+
<groupId>org.apache.httpcomponents</groupId>
420+
<artifactId>httpclient</artifactId>
421+
</exclusion>
422+
<exclusion>
423+
<groupId>org.slf4j</groupId>
424+
<artifactId>slf4j-api</artifactId>
425+
</exclusion>
426+
<exclusion>
427+
<groupId>javax.xml.bind</groupId>
428+
<artifactId>jaxb-api</artifactId>
429+
</exclusion>
430+
</exclusions>
413431
</dependency>
414432
<dependency>
415433
<groupId>org.thymeleaf</groupId>

serenity-report-resources/src/main/resources/report-resources/css/core.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,3 +2745,7 @@ div.code {
27452745
.dataTables_filter input {
27462746
font-weight: normal;
27472747
}
2748+
2749+
.scenario-docs.card-header.failure a.tagLink {
2750+
color: bisque;
2751+
}

serenity-rest-assured/pom.xml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,9 @@
7171
<artifactId>groovy</artifactId>
7272
</dependency>
7373
<dependency>
74-
<groupId>com.github.tomakehurst</groupId>
74+
<groupId>org.wiremock</groupId>
7575
<artifactId>wiremock</artifactId>
76-
<version>${wiremock.version}</version>
7776
<scope>test</scope>
78-
<exclusions>
79-
<exclusion>
80-
<groupId>com.google.guava</groupId>
81-
<artifactId>guava</artifactId>
82-
</exclusion>
83-
<exclusion>
84-
<groupId>org.apache.httpcomponents</groupId>
85-
<artifactId>httpclient</artifactId>
86-
</exclusion>
87-
<exclusion>
88-
<groupId>org.slf4j</groupId>
89-
<artifactId>slf4j-api</artifactId>
90-
</exclusion>
91-
<exclusion>
92-
<groupId>javax.xml.bind</groupId>
93-
<artifactId>jaxb-api</artifactId>
94-
</exclusion>
95-
<exclusion>
96-
<groupId>org.eclipse.jetty</groupId>
97-
<artifactId>jetty-util</artifactId>
98-
</exclusion>
99-
<exclusion>
100-
<groupId>org.eclipse.jetty</groupId>
101-
<artifactId>jetty-io</artifactId>
102-
</exclusion>
103-
</exclusions>
10477
</dependency>
10578
<!-- TEST DEPENDENCIES -->
10679
<dependency>

serenity-screenplay-rest/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ dependencies {
77
implementation project(':serenity-core')
88
implementation project(':serenity-rest-assured')
99
implementation project(':serenity-screenplay')
10-
testImplementation "junit:junit:${junitVersion}"
11-
testImplementation project(':serenity-junit')
10+
testImplementation project(':serenity-junit5')
1211

1312
// Unit testing
1413
testImplementation "org.junit.jupiter:junit-jupiter-api:${junit5Version}"
15-
testImplementation "org.junit.vintage:junit-vintage-engine:${junit5Version}"
14+
testImplementation "org.junit.vintage:junit-jupiter-engine:${junit5Version}"
15+
testImplementation "org.wiremock:wiremock"
1616
}

serenity-screenplay-rest/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
<version>${project.version}</version>
4444
</dependency>
4545
<dependency>
46-
<groupId>junit</groupId>
47-
<artifactId>junit</artifactId>
46+
<groupId>org.wiremock</groupId>
47+
<artifactId>wiremock</artifactId>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
5151
<groupId>${project.groupId}</groupId>
52-
<artifactId>serenity-junit</artifactId>
52+
<artifactId>serenity-junit5</artifactId>
5353
<version>${project.version}</version>
5454
<scope>test</scope>
5555
</dependency>
@@ -61,8 +61,8 @@
6161
<scope>test</scope>
6262
</dependency>
6363
<dependency>
64-
<groupId>org.junit.vintage</groupId>
65-
<artifactId>junit-vintage-engine</artifactId>
64+
<groupId>org.junit.jupiter</groupId>
65+
<artifactId>junit-jupiter-engine</artifactId>
6666
<scope>test</scope>
6767
</dependency>
6868
<dependency>

serenity-screenplay-rest/src/test/java/net/serenitybdd/screenplay/rest/examples/WhenInteractingWithAnAPIUsingScreenplay.java

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package net.serenitybdd.screenplay.rest.examples;
22

3-
import net.serenitybdd.junit.runners.SerenityRunner;
3+
import com.github.tomakehurst.wiremock.client.WireMock;
4+
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
5+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
6+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
7+
import net.serenitybdd.junit5.SerenityJUnit5Extension;
8+
import net.serenitybdd.rest.RestDefaults;
49
import net.serenitybdd.rest.SerenityRest;
510
import net.serenitybdd.screenplay.Actor;
611
import net.serenitybdd.screenplay.Question;
@@ -14,24 +19,52 @@
1419
import net.serenitybdd.screenplay.rest.questions.RestQuestionBuilder;
1520
import net.serenitybdd.screenplay.rest.questions.TheResponse;
1621
import net.serenitybdd.annotations.Step;
17-
import org.junit.Test;
18-
import org.junit.runner.RunWith;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.junit.jupiter.api.extension.RegisterExtension;
1927

2028
import static net.serenitybdd.screenplay.GivenWhenThen.seeThat;
2129
import static net.serenitybdd.screenplay.Tasks.instrumented;
2230
import static net.serenitybdd.screenplay.rest.questions.ResponseConsequence.seeThatResponse;
2331
import static org.assertj.core.api.Assertions.assertThat;
2432
import static org.hamcrest.Matchers.*;
2533

26-
@RunWith(SerenityRunner.class)
34+
import io.restassured.builder.RequestSpecBuilder;
35+
36+
@ExtendWith(SerenityJUnit5Extension.class)
2737
public class WhenInteractingWithAnAPIUsingScreenplay {
2838

29-
Actor sam = Actor.named("Sam the supervisor").whoCan(CallAnApi.at("https://reqres.in"));
39+
private static final String REQRES_BASE_URL = "https://reqres.in";
40+
41+
// The ENABLE_RECORDING option allows capturing requests and responses via wiremock and requires proper access
42+
// to the external sites. There is some manual cleanup of the stubs after recording so in case of adding new
43+
// endpoints - you'd probably want to keep the existing stubs and run tests to hit the new endpoints only.
44+
private static final boolean ENABLE_RECORDING = false;
45+
private static final String MOCKED_SITES_CLASSPATH = "wiremock/proxied-sites";
46+
private static final String MOCKED_SITES_ROOT = "src/test/resources/" + MOCKED_SITES_CLASSPATH;
47+
48+
Actor sam;
49+
50+
@RegisterExtension
51+
static WireMockExtension wireMockExtension = WireMockExtension.newInstance()
52+
.options(prepareWireMockConfiguration())
53+
.proxyMode(true)
54+
.build();
55+
56+
@BeforeEach
57+
void prepare() {
58+
RestDefaults.reset(); // Reset previous settings, if any. Ensures that e.g headers are not repeated.
59+
RestDefaults.setDefaultRequestSpecification(getDefaultRequestSpecBuilder().build());
60+
61+
sam = Actor.named("Sam the supervisor").whoCan(CallAnApi.at(REQRES_BASE_URL));
62+
}
3063

3164
@Test
3265
public void list_all_users() {
3366

34-
Actor sam = Actor.named("Sam the supervisor").whoCan(CallAnApi.at("https://reqres.in"));
67+
Actor sam = Actor.named("Sam the supervisor").whoCan(CallAnApi.at(REQRES_BASE_URL));
3568

3669
sam.attemptsTo(
3770
FetchUser.withId(1),
@@ -181,7 +214,7 @@ public void question_via_query_params() {
181214
}
182215

183216
@Test
184-
public void using_query_parameters() {
217+
public void using_path_parameters() {
185218

186219
sam.attemptsTo(
187220
Get.resource("/api/users/{id}").with( request -> request.pathParam("id", 1))
@@ -200,7 +233,8 @@ public void user_not_found() {
200233
);
201234

202235
sam.should(
203-
seeThatResponse(response -> response.statusCode(not(equalTo(200))))
236+
seeThatResponse(response -> response.statusCode(not(equalTo(200)))),
237+
seeThatResponse(response -> response.statusCode(equalTo(404)))
204238
);
205239
}
206240

@@ -259,4 +293,51 @@ public void login() {
259293

260294
assertThat(token).isEqualTo("QpwL5tke4Pnpja7X4");
261295
}
296+
297+
private static WireMockConfiguration prepareWireMockConfiguration() {
298+
final WireMockConfiguration options = WireMockConfiguration.options()
299+
.dynamicPort()
300+
.withRootDirectory(MOCKED_SITES_ROOT)
301+
.enableBrowserProxying(ENABLE_RECORDING);
302+
303+
if (!ENABLE_RECORDING) {
304+
options.limitProxyTargets(NetworkAddressRules.builder().deny("*").build());
305+
options.usingFilesUnderClasspath(MOCKED_SITES_CLASSPATH);
306+
}
307+
308+
return options;
309+
}
310+
311+
private static RequestSpecBuilder getDefaultRequestSpecBuilder() {
312+
final RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder()
313+
.setProxy(wireMockExtension.getPort())
314+
.setRelaxedHTTPSValidation();
315+
316+
if (ENABLE_RECORDING) {
317+
requestSpecBuilder.addHeader("x-api-key", "reqres-free-v1");
318+
}
319+
320+
return requestSpecBuilder;
321+
}
322+
323+
@BeforeEach
324+
void prepareWireMock() {
325+
if (ENABLE_RECORDING) {
326+
wireMockExtension.startRecording(WireMock.recordSpec()
327+
.captureHeader("Host", true)
328+
.extractBinaryBodiesOver(0)
329+
.extractTextBodiesOver(0)
330+
.ignoreRepeatRequests()
331+
.allowNonProxied(false)
332+
.makeStubsPersistent(true)
333+
);
334+
}
335+
}
336+
337+
@AfterEach
338+
void tearDownWireMock() {
339+
if (ENABLE_RECORDING) {
340+
wireMockExtension.stopRecording();
341+
}
342+
}
262343
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,84 @@
11
package net.serenitybdd.screenplay.rest.examples;
22

33
import io.restassured.filter.session.SessionFilter;
4-
import net.serenitybdd.junit.runners.SerenityRunner;
4+
5+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
6+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
7+
import net.serenitybdd.junit5.SerenityJUnit5Extension;
58
import net.serenitybdd.screenplay.Actor;
69
import net.serenitybdd.screenplay.rest.abilities.CallAnApi;
710
import net.serenitybdd.screenplay.rest.interactions.Get;
8-
import org.junit.Test;
9-
import org.junit.runner.RunWith;
11+
import org.hamcrest.Matchers;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
import org.junit.jupiter.api.extension.RegisterExtension;
1016

1117
import static net.serenitybdd.screenplay.rest.questions.ResponseConsequence.seeThatResponse;
1218

13-
@RunWith(SerenityRunner.class)
19+
@ExtendWith(SerenityJUnit5Extension.class)
1420
public class WhenUsingSessionFilterRegressionTest {
1521

16-
Actor actor = Actor.named("actor").whoCan(CallAnApi.at("https://reqres.in"));
22+
@RegisterExtension
23+
static WireMockExtension sessionsWireMock = WireMockExtension.newInstance().options(WireMockConfiguration.options()
24+
.dynamicPort()
25+
.usingFilesUnderClasspath("wiremock/sessions"))
26+
.build();
27+
28+
Actor actor;
29+
30+
@BeforeEach
31+
void prepare() {
32+
actor = Actor.named("actor").whoCan(CallAnApi.at(sessionsWireMock.getRuntimeInfo().getHttpBaseUrl()));
33+
}
1734

1835
@Test
1936
public void didThrowNPE() {
20-
actor.attemptsTo(Get.resource("/").with(request -> request.filter(new SessionFilter())));
37+
actor.attemptsTo(Get.resource("/session-check").with(request -> request.filter(new SessionFilter())));
2138
actor.should(seeThatResponse("bad request", response -> response.statusCode(200)));
22-
2339
}
40+
2441
@Test
2542
public void didSucceed() {
26-
actor.attemptsTo(Get.resource("/").with(request -> {
43+
actor.attemptsTo(Get.resource("/session-check").with(request -> {
2744
request.filter(new SessionFilter());
2845
return request;
2946
}));
3047
actor.should(seeThatResponse("bad request", response -> response.statusCode(200)));
3148
}
32-
}
49+
50+
@Test
51+
void canStoreSessionUsingSessionFilter() {
52+
final SessionFilter sessionFilter = new SessionFilter();
53+
54+
actor.attemptsTo(Get.resource("/session-check").with(request -> request.filter(sessionFilter)));
55+
actor.should(
56+
seeThatResponse("successful response", response -> response.statusCode(200)),
57+
seeThatResponse(response -> response.body("message", Matchers.equalTo("New session started"))),
58+
seeThatResponse(response -> response.cookie("JSESSIONID", Matchers.notNullValue()))
59+
);
60+
61+
// The 2nd attempt leads to a different response body
62+
actor.attemptsTo(Get.resource("/session-check").with(request -> request.filter(sessionFilter)));
63+
actor.should(
64+
seeThatResponse("successful response", response -> response.statusCode(200)),
65+
seeThatResponse(response -> response.body("message", Matchers.equalTo("Hi! Welcome back!")))
66+
);
67+
}
68+
69+
@Test
70+
void sessionIsNotStoredWithoutSessionFilter() {
71+
actor.attemptsTo(Get.resource("/session-check"));
72+
actor.should(
73+
seeThatResponse("successful response", response -> response.statusCode(200)),
74+
seeThatResponse(response -> response.body("message", Matchers.equalTo("New session started")))
75+
);
76+
77+
// The 2nd attempt leads to the same response
78+
actor.attemptsTo(Get.resource("/session-check"));
79+
actor.should(
80+
seeThatResponse("successful response", response -> response.statusCode(200)),
81+
seeThatResponse(response -> response.body("message", Matchers.equalTo("New session started")))
82+
);
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"token": "QpwL5tke4Pnpja7X4"
3+
}

0 commit comments

Comments
 (0)