Skip to content

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

@u6f6o

Description

@u6f6o

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions