eclipse-vertx / eclipse-vertx/vertx-junit5
Allow to inject manually configured Vertx instance in test methods if needed
- Dominant language
- Java
- Stars
- 43
- Forks
- 33
- Avg merge
- 1m
- Merged PRs (30d)
- 1
Description
I'd like to propose to adapt the `VertxExtension` in order to allow a manual configuration of the `Vertx` instance before injecting it in the test/setup/teardown methods.
Junit5 offers a `@RegisterExtension` annotation that allows to configure extension objects. More information can be found [here](https://junit.org/junit5/docs/current/user-guide/#extensions-registration-programmatic).
Thus, the current mechanics, to simply inject a `Vertx` instance would still work, while users who'd like to adapt the settings of the `Vertx` instance for a given test case could still benefit from the `VertxExtension`.
In order to verify my proposal, I created a copy of the existing `VertxExtension` and added/adapted following code:
1. Add a new field with a supplier that returns a `Vertx` instance on invocation
```
private final Supplier vertxSupplier;
```
2. Add a default constructor and a constructor with the supplier parameter
```
public VertxExtension() {
this(Vertx::vertx);
}
public VertxExtension(Supplier vertxSupplier) {
this.vertxSupplier = vertxSupplier;
}
```
3. Adapt the resolveParameter method to invoke the `vertxSupplier`
```
return getOrCreateScopedObject(
parameterContext,
extensionContext,
VERTX_INSTANCE_KEY,
VERTX_INSTANCE_CREATOR_KEY,
key -> new ConfigurableVertxExtension.ScopedObject<>(vertxSupplier.get(), closeRegularVertx()));
```
In the test class itself, we could provide a configured vertx instance instead of sticking with the default (kotlin code):
```
companion object {
@JvmField
@RegisterExtension
val ext = VertxExtension { manuallyConfiguredVertx() }
}
```
Hope you consider this proposal.
Contributor guide
Assessment
This issue has not been assessed yet.