Skip to content

Commit 5cd892d

Browse files
committed
Migrate vertx-unit code to vertx-junit5
Some portions of this content were created with the assistance of Claude Code.
1 parent e1b315a commit 5cd892d

File tree

9 files changed

+362
-394
lines changed

9 files changed

+362
-394
lines changed

vertx-web/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,19 @@
9191
</dependency>
9292
<dependency>
9393
<groupId>io.vertx</groupId>
94-
<artifactId>vertx-unit</artifactId>
94+
<artifactId>vertx-junit5</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.junit.jupiter</groupId>
99+
<artifactId>junit-jupiter-engine</artifactId>
100+
<version>5.14.0</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.junit.vintage</groupId>
105+
<artifactId>junit-vintage-engine</artifactId>
106+
<version>5.14.0</version>
95107
<scope>test</scope>
96108
</dependency>
97109
<dependency>
Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
11
package io.vertx.ext.web.tests;
22

3+
import io.vertx.core.Vertx;
34
import io.vertx.core.http.*;
4-
import io.vertx.ext.unit.Async;
5-
import io.vertx.ext.unit.TestContext;
6-
import io.vertx.ext.unit.junit.RunTestOnContext;
7-
import io.vertx.ext.unit.junit.VertxUnitRunner;
85
import io.vertx.ext.web.Router;
96
import io.vertx.ext.web.handler.*;
10-
import org.junit.Before;
11-
import org.junit.Rule;
12-
import org.junit.Test;
13-
import org.junit.runner.RunWith;
7+
import io.vertx.junit5.VertxTest;
8+
import io.vertx.junit5.VertxTestContext;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
1411

15-
@RunWith(VertxUnitRunner.class)
16-
public class Router100ContinueTest {
12+
import java.util.concurrent.TimeUnit;
1713

18-
@Rule
19-
public final RunTestOnContext rule = new RunTestOnContext();
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
2015

21-
final Router router = Router.router(rule.vertx());
16+
@VertxTest
17+
public class Router100ContinueTest {
2218

19+
Vertx vertx;
20+
Router router;
2321
HttpServer server;
2422
HttpClient client;
2523

26-
@Before
27-
public void setup(TestContext should) {
28-
final Async setup = should.async();
29-
rule.vertx()
24+
@BeforeEach
25+
public void setup(Vertx vertx) throws Exception {
26+
this.vertx = vertx;
27+
this.router = Router.router(vertx);
28+
server = vertx
3029
.createHttpServer()
31-
.requestHandler(router)
32-
.listen(0)
33-
.onSuccess(server -> {
34-
this.server = server;
35-
this.client = rule.vertx()
36-
.createHttpClient(new HttpClientOptions().setDefaultPort(server.actualPort()).setDefaultHost("localhost"));
37-
setup.complete();
38-
})
39-
.onFailure(should::fail);
30+
.requestHandler(router);
31+
server.listen(0).await(20, TimeUnit.SECONDS);
32+
client = vertx
33+
.createHttpClient(new HttpClientOptions().setDefaultPort(server.actualPort()).setDefaultHost("localhost"));
4034
}
4135

4236
@Test
43-
public void testContinue(TestContext should) {
44-
final Async test = should.async();
37+
public void testContinue(VertxTestContext testContext) {
4538
router.route()
4639
.handler(BodyHandler.create())
4740
.handler(ctx -> {
48-
should.assertEquals("DATA", ctx.body().asString());
41+
assertEquals("DATA", ctx.body().asString());
4942
ctx.end();
5043
});
5144

5245
client.request(HttpMethod.POST, "/")
53-
.onFailure(should::fail)
46+
.onFailure(testContext::failNow)
5447
.onSuccess(req -> {
5548
req
5649
.response()
57-
.onFailure(should::fail)
50+
.onFailure(testContext::failNow)
5851
.onSuccess(res -> {
59-
should.assertEquals(200, res.statusCode());
60-
test.complete();
52+
assertEquals(200, res.statusCode());
53+
testContext.completeNow();
6154
});
6255

6356
req
@@ -66,31 +59,30 @@ public void testContinue(TestContext should) {
6659
.continueHandler(v ->
6760
req
6861
.end("DATA")
69-
.onFailure(should::fail))
62+
.onFailure(testContext::failNow))
7063
.sendHead()
71-
.onFailure(should::fail);
64+
.onFailure(testContext::failNow);
7265
});
7366
}
7467

7568
@Test
76-
public void testBadExpectation(TestContext should) {
77-
final Async test = should.async();
69+
public void testBadExpectation(VertxTestContext testContext) {
7870
router.route()
7971
.handler(BodyHandler.create())
8072
.handler(ctx -> {
81-
should.assertEquals("DATA", ctx.body().asString());
73+
assertEquals("DATA", ctx.body().asString());
8274
ctx.end();
8375
});
8476

8577
client.request(HttpMethod.POST, "/")
86-
.onFailure(should::fail)
78+
.onFailure(testContext::failNow)
8779
.onSuccess(req -> {
8880
req
8981
.response()
90-
.onFailure(should::fail)
82+
.onFailure(testContext::failNow)
9183
.onSuccess(res -> {
92-
should.assertEquals(417, res.statusCode());
93-
test.complete();
84+
assertEquals(417, res.statusCode());
85+
testContext.completeNow();
9486
});
9587

9688
req
@@ -99,32 +91,31 @@ public void testBadExpectation(TestContext should) {
9991
.continueHandler(v ->
10092
req
10193
.end("DATA")
102-
.onFailure(should::fail))
94+
.onFailure(testContext::failNow))
10395
.sendHead()
104-
.onFailure(should::fail);
96+
.onFailure(testContext::failNow);
10597
});
10698
}
10799

108100
@Test
109-
public void testExpectButTooLarge(TestContext should) {
110-
final Async test = should.async();
101+
public void testExpectButTooLarge(VertxTestContext testContext) {
111102
router.route()
112103
.handler(BodyHandler.create().setBodyLimit(1))
113104
.handler(ctx -> {
114-
should.assertEquals("DATA", ctx.body().asString());
105+
assertEquals("DATA", ctx.body().asString());
115106
ctx.end();
116107
});
117108

118109
client.request(HttpMethod.POST, "/")
119-
.onFailure(should::fail)
110+
.onFailure(testContext::failNow)
120111
.onSuccess(req -> {
121112
req
122113
.response()
123-
.onFailure(should::fail)
114+
.onFailure(testContext::failNow)
124115
.onSuccess(res -> {
125116
// entity too large
126-
should.assertEquals(413, res.statusCode());
127-
test.complete();
117+
assertEquals(413, res.statusCode());
118+
testContext.completeNow();
128119
});
129120

130121
req
@@ -133,9 +124,9 @@ public void testExpectButTooLarge(TestContext should) {
133124
.continueHandler(v ->
134125
req
135126
.end("DATA")
136-
.onFailure(should::fail))
127+
.onFailure(testContext::failNow))
137128
.sendHead()
138-
.onFailure(should::fail);
129+
.onFailure(testContext::failNow);
139130
});
140131
}
141132
}
Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,122 @@
11
package io.vertx.ext.web.tests;
22

3+
import io.vertx.core.Vertx;
34
import io.vertx.ext.auth.authorization.RoleBasedAuthorization;
45
import io.vertx.ext.auth.oauth2.OAuth2Auth;
56
import io.vertx.ext.auth.oauth2.OAuth2Options;
6-
import io.vertx.ext.unit.junit.RunTestOnContext;
7-
import io.vertx.ext.unit.junit.VertxUnitRunner;
87
import io.vertx.ext.web.Router;
98
import io.vertx.ext.web.RoutingContext;
109
import io.vertx.ext.web.handler.*;
1110
import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions;
1211
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
1312
import io.vertx.ext.web.sstore.SessionStore;
14-
import org.junit.Rule;
15-
import org.junit.Test;
16-
import org.junit.runner.RunWith;
13+
import io.vertx.junit5.VertxTest;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1716

18-
@RunWith(VertxUnitRunner.class)
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
19+
@VertxTest
1920
public class RouterValidationTest {
2021

21-
@Rule
22-
public final RunTestOnContext rule = new RunTestOnContext();
22+
Vertx vertx;
23+
24+
@BeforeEach
25+
public void setUp(Vertx vertx) {
26+
this.vertx = vertx;
27+
}
2328

2429
@Test
2530
public void addBodyHandler() {
26-
Router router = Router.router(rule.vertx());
31+
Router router = Router.router(vertx);
2732
router.route().handler(BodyHandler.create());
2833
}
2934

3035
@Test
3136
public void addBodyHandlerAndUserHandler() {
32-
Router router = Router.router(rule.vertx());
37+
Router router = Router.router(vertx);
3338
router.route()
3439
.handler(BodyHandler.create())
3540
.handler(RoutingContext::end);
3641
}
3742

38-
@Test(expected = IllegalStateException.class)
43+
@Test
3944
public void addBodyHandlerAndUserHandlerBadOrder() {
40-
Router router = Router.router(rule.vertx());
41-
router.route()
42-
.handler(RoutingContext::end)
43-
.handler(BodyHandler.create());
45+
Router router = Router.router(vertx);
46+
assertThrows(IllegalStateException.class, () -> {
47+
router.route()
48+
.handler(RoutingContext::end)
49+
.handler(BodyHandler.create());
50+
});
4451
}
4552

4653
@Test
4754
public void addBodyHandlerAndAuthn() {
48-
Router router = Router.router(rule.vertx());
55+
Router router = Router.router(vertx);
4956
router.route()
5057
.handler(BodyHandler.create())
51-
.handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id"))));
58+
.handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id"))));
5259
}
5360

5461
@Test
5562
public void addBodyHandlerAndAuthnAuthz() {
56-
Router router = Router.router(rule.vertx());
63+
Router router = Router.router(vertx);
5764
router.route()
58-
.handler(SessionHandler.create(SessionStore.create(rule.vertx())))
59-
.handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id"))))
65+
.handler(SessionHandler.create(SessionStore.create(vertx)))
66+
.handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id"))))
6067
.handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role")));
6168
}
6269

63-
@Test(expected = IllegalStateException.class)
70+
@Test
6471
public void addBodyHandlerAndAuthnAuthzBadOrder() {
65-
Router router = Router.router(rule.vertx());
66-
router.route()
67-
.handler(SessionHandler.create(SessionStore.create(rule.vertx())))
68-
.handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role")))
69-
// will fail as authz depends on authn (so adding authn after authz is clearly a mistake)
70-
.handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id"))));
72+
Router router = Router.router(vertx);
73+
// will fail as authz depends on authn (so adding authn after authz is clearly a mistake)
74+
assertThrows(IllegalStateException.class, () -> {
75+
router.route()
76+
.handler(SessionHandler.create(SessionStore.create(vertx)))
77+
.handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role")))
78+
.handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id"))));
79+
});
7180
}
7281

73-
@Test(expected = IllegalStateException.class)
82+
@Test
7483
public void addBodyHandlerAndAuthnAuthzBadOrder2() {
75-
Router router = Router.router(rule.vertx());
76-
router.route()
77-
.handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id"))))
78-
.handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role")))
79-
// will fail as platform handlers should be mounted earlier as they have dependants
80-
.handler(SessionHandler.create(SessionStore.create(rule.vertx())));
84+
Router router = Router.router(vertx);
85+
assertThrows(IllegalStateException.class, () -> {
86+
router.route()
87+
.handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id"))))
88+
.handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role")))
89+
.handler(SessionHandler.create(SessionStore.create(vertx)));
90+
});
8191
}
8292

8393
@Test
8494
public void addSecurityPolicyAndUserHandlers() {
85-
Router router = Router.router(rule.vertx());
95+
Router router = Router.router(vertx);
8696
router.route()
8797
.handler(CorsHandler.create())
88-
.subRouter(SockJSHandler.create(rule.vertx()).bridge(new SockJSBridgeOptions()));
98+
.subRouter(SockJSHandler.create(vertx).bridge(new SockJSBridgeOptions()));
8999
}
90100

91-
@Test(expected = IllegalStateException.class)
101+
@Test
92102
public void addSecurityPolicyAndUserHandlersBadOrder() {
93-
Router router = Router.router(rule.vertx());
94-
router.route()
95-
.subRouter(SockJSHandler.create(rule.vertx()).bridge(new SockJSBridgeOptions()))
103+
Router router = Router.router(vertx);
104+
assertThrows(IllegalStateException.class, () -> {
96105
// will fail as the route should be exclusive
97-
.handler(CorsHandler.create());
106+
router.route()
107+
.subRouter(SockJSHandler.create(vertx).bridge(new SockJSBridgeOptions()))
108+
.handler(CorsHandler.create());
109+
});
98110
}
99111

100-
@Test(expected = IllegalStateException.class)
112+
@Test
101113
public void addSecurityPolicyAndUserHandlersBadOrder2() {
102-
Router router = Router.router(rule.vertx());
103-
router.route()
104-
.handler(RoutingContext::end)
114+
Router router = Router.router(vertx);
115+
assertThrows(IllegalStateException.class, () -> {
105116
// will fail as the route has a user handler but a policy one is being added after
106-
.handler(CorsHandler.create());
117+
router.route()
118+
.handler(RoutingContext::end)
119+
.handler(CorsHandler.create());
120+
});
107121
}
108122
}

0 commit comments

Comments
 (0)