Skip to content

Commit 250b6e7

Browse files
authored
Merge pull request vert-x3#262 from vert-x3/junit5
Vert.x + JUnit 5 examples
2 parents 0e2b4f7 + 48e57c8 commit 250b6e7

File tree

6 files changed

+308
-0
lines changed

6 files changed

+308
-0
lines changed

README.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ your Vert.x (or other asynchronous) applications.
148148

149149
The link:unit-examples/README.adoc[Vert.x Unit examples] shows how to use Vert.x Unit.
150150

151+
=== Vert.x JUnit 5 examples
152+
153+
The `vertx-junit5` modules allows testing Vert.x asynchronous operations with JUnit 5.
154+
155+
See link:junit5-examples/README.adoc[Vert.x JUnit 5 examples].
156+
151157
=== RxJava examples
152158

153159
Vert.x for RxJava provides most of its APIs as RxJava so you can use those if you prefer.

junit5-examples/README.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
= Vert.x JUnit 5 examples
2+
3+
This project illustrates how to use JUnit 5 with Vert.x for testing asynchronous operations.
4+
5+
link:pom.xml[pom.xml] declares the dependencies and configures the Maven Surefire plugin.
6+
Gradle users should check the documentation at http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle
7+
8+
The link:src/test/java/hello/SampleVerticleTest.java[SampleVerticleTest.java] file contains a few test cases
9+
of how to deal with asynchronous events (timers, deployments, network requests, etc).

junit5-examples/pom.xml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2018 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>io.vertx</groupId>
24+
<artifactId>junit5-examples</artifactId>
25+
<version>3.5.1-SNAPSHOT</version>
26+
27+
<properties>
28+
<junit-jupiter.version>5.0.1</junit-jupiter.version>
29+
<maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
30+
<junit-platform-surefire-provider.version>1.0.1</junit-platform-surefire-provider.version>
31+
<assertj-core.version>3.8.0</assertj-core.version>
32+
</properties>
33+
34+
<dependencyManagement>
35+
<dependencies>
36+
<dependency>
37+
<groupId>io.vertx</groupId>
38+
<artifactId>vertx-stack-depchain</artifactId>
39+
<version>${project.version}</version>
40+
<type>pom</type>
41+
<scope>import</scope>
42+
</dependency>
43+
</dependencies>
44+
</dependencyManagement>
45+
46+
<dependencies>
47+
48+
<!-- Vert.x + web client -->
49+
<dependency>
50+
<groupId>io.vertx</groupId>
51+
<artifactId>vertx-core</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.vertx</groupId>
55+
<artifactId>vertx-web-client</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>ch.qos.logback</groupId>
59+
<artifactId>logback-classic</artifactId>
60+
<version>1.2.3</version>
61+
</dependency>
62+
63+
<!-- Test dependencies -->
64+
<dependency>
65+
<groupId>io.vertx</groupId>
66+
<artifactId>vertx-junit5</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.junit.jupiter</groupId>
71+
<artifactId>junit-jupiter-api</artifactId>
72+
<version>${junit-jupiter.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.junit.jupiter</groupId>
77+
<artifactId>junit-jupiter-params</artifactId>
78+
<version>${junit-jupiter.version}</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.assertj</groupId>
83+
<artifactId>assertj-core</artifactId>
84+
<version>${assertj-core.version}</version>
85+
<scope>test</scope>
86+
</dependency>
87+
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<artifactId>maven-compiler-plugin</artifactId>
94+
<version>3.1</version>
95+
<configuration>
96+
<source>1.8</source>
97+
<target>1.8</target>
98+
</configuration>
99+
</plugin>
100+
<plugin>
101+
<artifactId>maven-surefire-plugin</artifactId>
102+
<version>${maven-surefire-plugin.version}</version>
103+
<dependencies>
104+
<dependency>
105+
<groupId>org.junit.platform</groupId>
106+
<artifactId>junit-platform-surefire-provider</artifactId>
107+
<version>${junit-platform-surefire-provider.version}</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>org.junit.jupiter</groupId>
111+
<artifactId>junit-jupiter-engine</artifactId>
112+
<version>${junit-jupiter.version}</version>
113+
</dependency>
114+
</dependencies>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
119+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2018 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package hello;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.Future;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
/**
25+
* @author <a href="https://julien.ponge.org/">Julien Ponge</a>
26+
*/
27+
public class SampleVerticle extends AbstractVerticle {
28+
29+
private final Logger logger = LoggerFactory.getLogger(SampleVerticle.class);
30+
31+
@Override
32+
public void start(Future<Void> startFuture) {
33+
vertx.createHttpServer()
34+
.requestHandler(req -> {
35+
req.response()
36+
.putHeader("Content-Type", "plain/text")
37+
.end("Yo!");
38+
logger.info("Handled a request on path {} from {}", req.path(), req.remoteAddress().host());
39+
})
40+
.listen(11981, ar -> {
41+
if (ar.succeeded()) {
42+
startFuture.complete();
43+
} else {
44+
startFuture.fail(ar.cause());
45+
}
46+
});
47+
}
48+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2018 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package hello;
18+
19+
import io.vertx.core.Vertx;
20+
import io.vertx.core.VertxOptions;
21+
import io.vertx.ext.web.client.WebClient;
22+
import io.vertx.ext.web.codec.BodyCodec;
23+
import io.vertx.junit5.Checkpoint;
24+
import io.vertx.junit5.VertxExtension;
25+
import io.vertx.junit5.VertxTestContext;
26+
import org.assertj.core.api.Assertions;
27+
import org.junit.jupiter.api.*;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
30+
import java.util.concurrent.atomic.AtomicInteger;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* @author <a href="https://julien.ponge.org/">Julien Ponge</a>
36+
*/
37+
@DisplayName("👋 A fairly basic test example")
38+
@ExtendWith(VertxExtension.class)
39+
class SampleVerticleTest {
40+
41+
@Test
42+
@DisplayName("⏱ Count 3 timer ticks")
43+
void countThreeTicks(Vertx vertx, VertxTestContext testContext) {
44+
AtomicInteger counter = new AtomicInteger();
45+
vertx.setPeriodic(100, id -> {
46+
if (counter.incrementAndGet() == 3) {
47+
testContext.completeNow();
48+
}
49+
});
50+
}
51+
52+
@Test
53+
@DisplayName("⏱ Count 3 timer ticks, with a checkpoint")
54+
void countThreeTicksWithCheckpoints(Vertx vertx, VertxTestContext testContext) {
55+
Checkpoint checkpoint = testContext.checkpoint(3);
56+
vertx.setPeriodic(100, id -> checkpoint.flag());
57+
}
58+
59+
@Test
60+
@DisplayName("🚀 Deploy a HTTP service verticle and make 10 requests")
61+
void useSampleVerticle(Vertx vertx, VertxTestContext testContext) {
62+
WebClient webClient = WebClient.create(vertx);
63+
Checkpoint deploymentCheckpoint = testContext.checkpoint();
64+
Checkpoint requestCheckpoint = testContext.checkpoint(10);
65+
66+
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> {
67+
deploymentCheckpoint.flag();
68+
69+
for (int i = 0; i < 10; i++) {
70+
webClient.get(11981, "localhost", "/")
71+
.as(BodyCodec.string())
72+
.send(testContext.succeeding(resp -> {
73+
testContext.verify(() -> {
74+
assertThat(resp.statusCode()).isEqualTo(200);
75+
assertThat(resp.body()).contains("Yo!");
76+
requestCheckpoint.flag();
77+
});
78+
}));
79+
}
80+
}));
81+
}
82+
83+
@DisplayName("➡️ A nested test with customized lifecycle")
84+
@Nested
85+
class CustomLifecycleTest {
86+
87+
Vertx vertx;
88+
89+
@BeforeEach
90+
void prepare() {
91+
vertx = Vertx.vertx(new VertxOptions()
92+
.setMaxEventLoopExecuteTime(1000)
93+
.setPreferNativeTransport(true)
94+
.setFileResolverCachingEnabled(true));
95+
}
96+
97+
@Test
98+
@DisplayName("⬆️ Deploy SampleVerticle")
99+
void deploySampleVerticle(VertxTestContext testContext) {
100+
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> testContext.completeNow()));
101+
}
102+
103+
@Test
104+
@DisplayName("🛂 Make a HTTP client request to SampleVerticle")
105+
void httpRequest(VertxTestContext testContext) {
106+
WebClient webClient = WebClient.create(vertx);
107+
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> {
108+
webClient.get(11981, "localhost", "/yo")
109+
.as(BodyCodec.string())
110+
.send(testContext.succeeding(resp -> {
111+
testContext.verify(() -> {
112+
assertThat(resp.statusCode()).isEqualTo(200);
113+
assertThat(resp.body()).contains("Yo!");
114+
testContext.completeNow();
115+
});
116+
}));
117+
}));
118+
}
119+
120+
@AfterEach
121+
void cleanup() {
122+
vertx.close();
123+
}
124+
}
125+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<module>grpc-examples</module>
5151
<module>kafka-examples</module>
5252
<module>kotlin-examples/coroutines</module>
53+
<module>junit5-examples</module>
5354
</modules>
5455

5556
<profiles>

0 commit comments

Comments
 (0)