eclipse-vertx / vertx-junit5

Testing Vert.x applications with JUnit 5
Apache License 2.0
44 stars 32 forks source link

Allow to inject manually configured Vertx instance in test methods if needed #55

Open u6f6o opened 5 years ago

u6f6o commented 5 years ago

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.

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<Vertx> vertxSupplier;
  2. Add a default constructor and a constructor with the supplier parameter
    public VertxExtension() {
     this(Vertx::vertx);
    }
    public VertxExtension(Supplier<Vertx> 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.

jponge commented 5 years ago

On paper this sounds interesting, feel-free to propose a pull-request so we can concretely review it 😉

u6f6o commented 5 years ago

👍 done: #56

slinkydeveloper commented 3 years ago

It seems like this issue is still not solved, and we have a duplicate: https://github.com/vert-x3/vertx-junit5/issues/70