11
11
import java .util .concurrent .CompletionStage ;
12
12
import java .util .concurrent .TimeUnit ;
13
13
import java .util .function .Supplier ;
14
+
15
+ import io .vertx .junit5 .VertxTestContext ;
14
16
import jakarta .persistence .criteria .CriteriaQuery ;
15
17
16
18
import org .hibernate .SessionFactory ;
22
24
import org .hibernate .reactive .provider .Settings ;
23
25
import org .hibernate .reactive .stage .Stage ;
24
26
25
- import org .junit .After ;
26
- import org .junit .AfterClass ;
27
- import org .junit .Before ;
28
- import org .junit .ClassRule ;
29
- import org .junit .runner .RunWith ;
27
+ import org .junit .jupiter .api .AfterAll ;
28
+ import org .junit .jupiter .api .AfterEach ;
29
+ import org .junit .jupiter .api .BeforeEach ;
30
+ import org .junit .jupiter .api .TestInstance ;
31
+ import org .junit .jupiter .api .extension .ExtendWith ;
32
+ import org .junit .jupiter .api .extension .RegisterExtension ;
30
33
31
34
import io .smallrye .mutiny .Uni ;
32
35
import io .vertx .core .Promise ;
33
- import io .vertx .core .Vertx ;
34
36
import io .vertx .core .VertxOptions ;
35
- import io .vertx .ext .unit .Async ;
36
- import io .vertx .ext .unit .TestContext ;
37
- import io .vertx .ext .unit .junit .RunTestOnContext ;
38
- import io .vertx .ext .unit .junit .Timeout ;
39
- import io .vertx .ext .unit .junit .VertxUnitRunner ;
37
+ import io .vertx .junit5 .RunTestOnContext ;
38
+ import io .vertx .junit5 .Timeout ;
39
+ import io .vertx .junit5 .VertxExtension ;
40
40
import org .testcontainers .containers .PostgreSQLContainer ;
41
41
import org .testcontainers .utility .DockerImageName ;
42
42
47
47
* Similar to BaseReactiveTest in the hibernate-reactive-core.
48
48
* Hopefully, one day we will reorganize the code better.
49
49
*/
50
- @ RunWith (VertxUnitRunner .class )
50
+ @ ExtendWith (VertxExtension .class )
51
+ @ TestInstance (TestInstance .Lifecycle .PER_METHOD )
52
+ @ Timeout (value = 600 , timeUnit = TimeUnit .SECONDS )
51
53
public abstract class BaseReactiveIT {
52
54
53
55
// These properties are in DatabaseConfiguration in core
@@ -67,17 +69,20 @@ public abstract class BaseReactiveIT {
67
69
.withDatabaseName ( DB_NAME )
68
70
.withReuse ( true );
69
71
70
- @ ClassRule
71
- public static final Timeout rule = Timeout .seconds ( 10 * 60 );
72
72
73
73
private static SessionFactory ormSessionFactory ;
74
- @ ClassRule
75
- public static final RunTestOnContext vertxContextRule = new RunTestOnContext ( () -> {
76
- VertxOptions options = new VertxOptions ();
77
- options .setBlockedThreadCheckInterval ( 5 );
78
- options .setBlockedThreadCheckIntervalUnit ( TimeUnit .MINUTES );
79
- return Vertx .vertx ( options );
80
- } );
74
+
75
+ /**
76
+ * Configure Vertx JUnit5 test context
77
+ */
78
+ @ RegisterExtension
79
+ static RunTestOnContext testOnContext = new RunTestOnContext ( vertxOptions () );
80
+
81
+ private static VertxOptions vertxOptions () {
82
+ return new VertxOptions ()
83
+ .setBlockedThreadCheckInterval ( 5 )
84
+ .setBlockedThreadCheckIntervalUnit ( TimeUnit .MINUTES );
85
+ }
81
86
82
87
/**
83
88
* Configure properties defined in {@link Settings}.
@@ -113,10 +118,6 @@ private static String dbConnectionUrl(boolean enableDocker) {
113
118
return "postgres://localhost:5432/" + DB_NAME ;
114
119
}
115
120
116
- protected static void test (TestContext context , CompletionStage <?> work ) {
117
- test ( context .async (), context , work );
118
- }
119
-
120
121
/**
121
122
* These entities will be added to the configuration of the factory and
122
123
* the rows in the mapping tables deleted after each test.
@@ -135,29 +136,19 @@ protected Collection<String> mappings() {
135
136
return List .of ();
136
137
}
137
138
138
- /**
139
- * For when we need to create the {@link Async} in advance
140
- */
141
- protected static void test (Async async , TestContext context , CompletionStage <?> work ) {
139
+ public static void test (VertxTestContext context , CompletionStage <?> work ) {
142
140
work .whenComplete ( (res , err ) -> {
143
141
if ( err != null ) {
144
- context .fail ( err );
142
+ context .failNow ( err );
145
143
}
146
144
else {
147
- async . complete ();
145
+ context . completeNow ();
148
146
}
149
147
} );
150
148
}
151
149
152
- protected static void test (TestContext context , Uni <?> uni ) {
153
- test ( context .async (), context , uni );
154
- }
155
-
156
- /**
157
- * For when we need to create the {@link Async} in advance
158
- */
159
- public static void test (Async async , TestContext context , Uni <?> uni ) {
160
- uni .subscribe ().with ( res -> async .complete (), context ::fail );
150
+ public static void test (VertxTestContext context , Uni <?> uni ) {
151
+ uni .subscribe ().with ( res -> context .completeNow (), context ::failNow );
161
152
}
162
153
163
154
protected Configuration constructConfiguration () {
@@ -193,8 +184,8 @@ private <T> CriteriaQuery<T> queryForDelete(Class<T> entityClass) {
193
184
return query ;
194
185
}
195
186
196
- @ Before
197
- public void before (TestContext context ) {
187
+ @ BeforeEach
188
+ public void before (VertxTestContext context ) {
198
189
test ( context , setupSessionFactory ( this ::constructConfiguration ) );
199
190
}
200
191
@@ -212,9 +203,9 @@ protected CompletionStage<Void> setupSessionFactory(Configuration configuration)
212
203
* @param confSupplier supplies the configuration for the factory
213
204
* @return a {@link CompletionStage} void that succeeds when the factory is ready.
214
205
*/
215
- protected CompletionStage <Void > setupSessionFactory (Supplier <Configuration > confSupplier ) {
206
+ protected static CompletionStage <Void > setupSessionFactory (Supplier <Configuration > confSupplier ) {
216
207
CompletableFuture <Void > future = new CompletableFuture <>();
217
- vertxContextRule .vertx ()
208
+ testOnContext .vertx ()
218
209
.executeBlocking (
219
210
// schema generation is a blocking operation and so it causes an
220
211
// exception when run on the Vert.x event loop. So call it using
@@ -232,7 +223,7 @@ protected CompletionStage<Void> setupSessionFactory(Supplier<Configuration> conf
232
223
return future ;
233
224
}
234
225
235
- private void startFactoryManager (Promise <Object > p , Supplier <Configuration > confSupplier ) {
226
+ private static void startFactoryManager (Promise <Object > p , Supplier <Configuration > confSupplier ) {
236
227
try {
237
228
ormSessionFactory = createHibernateSessionFactory ( confSupplier .get () );
238
229
p .complete ();
@@ -242,7 +233,7 @@ private void startFactoryManager(Promise<Object> p, Supplier<Configuration> conf
242
233
}
243
234
}
244
235
245
- private SessionFactory createHibernateSessionFactory (Configuration configuration ) {
236
+ private static SessionFactory createHibernateSessionFactory (Configuration configuration ) {
246
237
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder ()
247
238
.applySettings ( configuration .getProperties () );
248
239
addServices ( builder );
@@ -251,13 +242,13 @@ private SessionFactory createHibernateSessionFactory(Configuration configuration
251
242
return configuration .buildSessionFactory ( registry );
252
243
}
253
244
254
- protected void addServices (StandardServiceRegistryBuilder builder ) {}
245
+ protected static void addServices (StandardServiceRegistryBuilder builder ) {}
255
246
256
- protected void configureServices (StandardServiceRegistry registry ) {
247
+ protected static void configureServices (StandardServiceRegistry registry ) {
257
248
}
258
249
259
- @ After
260
- public void after (TestContext context ) {
250
+ @ AfterEach
251
+ public void after (VertxTestContext context ) {
261
252
test ( context , cleanDb () );
262
253
}
263
254
@@ -271,8 +262,8 @@ protected CompletionStage<Void> cleanDb() {
271
262
: deleteEntities ( classes .toArray ( new Class <?>[0 ] ) );
272
263
}
273
264
274
- @ AfterClass
275
- public static void closeFactory (TestContext context ) {
265
+ @ AfterAll
266
+ public static void closeFactory () {
276
267
if ( ormSessionFactory != null && ormSessionFactory .isOpen () ) {
277
268
ormSessionFactory .close ();
278
269
}
0 commit comments