Skip to content

Commit 276b10a

Browse files
committed
Vert.x + JUnit 5 examples
1 parent 0e2b4f7 commit 276b10a

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.ext.web.client.WebClient;
21+
import io.vertx.ext.web.codec.BodyCodec;
22+
import io.vertx.junit5.Checkpoint;
23+
import io.vertx.junit5.VertxExtension;
24+
import io.vertx.junit5.VertxTestContext;
25+
import org.assertj.core.api.Assertions;
26+
import org.junit.jupiter.api.DisplayName;
27+
import org.junit.jupiter.api.RepeatedTest;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
31+
import java.util.concurrent.atomic.AtomicInteger;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* @author <a href="https://julien.ponge.org/">Julien Ponge</a>
37+
*/
38+
@DisplayName("👋 A fairly basic test example")
39+
@ExtendWith(VertxExtension.class)
40+
class SampleVerticleTest {
41+
42+
@Test
43+
@DisplayName("⏱ Count 3 timer ticks")
44+
void countThreeTicks(Vertx vertx, VertxTestContext testContext) {
45+
AtomicInteger counter = new AtomicInteger();
46+
vertx.setPeriodic(100, id -> {
47+
if (counter.incrementAndGet() == 3) {
48+
testContext.completeNow();
49+
}
50+
});
51+
}
52+
53+
@Test
54+
@DisplayName("⏱ Count 3 timer ticks, with a checkpoint")
55+
void countThreeTicksWithCheckpoints(Vertx vertx, VertxTestContext testContext) {
56+
Checkpoint checkpoint = testContext.checkpoint(3);
57+
vertx.setPeriodic(100, id -> checkpoint.flag());
58+
}
59+
60+
@Test
61+
@DisplayName("🚀 Deploy a HTTP service verticle and make 10 requests")
62+
void useSampleVerticle(Vertx vertx, VertxTestContext testContext) {
63+
WebClient webClient = WebClient.create(vertx);
64+
Checkpoint deploymentCheckpoint = testContext.checkpoint();
65+
Checkpoint requestCheckpoint = testContext.checkpoint(10);
66+
67+
vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> {
68+
deploymentCheckpoint.flag();
69+
70+
for (int i = 0; i < 10; i++) {
71+
webClient.get(11981, "localhost", "/")
72+
.as(BodyCodec.string())
73+
.send(testContext.succeeding(resp -> {
74+
testContext.verify(() -> {
75+
assertThat(resp.statusCode()).isEqualTo(200);
76+
assertThat(resp.body()).contains("Yo!");
77+
requestCheckpoint.flag();
78+
});
79+
}));
80+
}
81+
}));
82+
}
83+
}

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)