Skip to content

Expose test method to test instance constructor extensions #3670

@michaeldimchuk

Description

@michaeldimchuk

I know this was previously discussed on #1568, but the discussion wasn't conclusive and the team seemed open to the change, so I'd like to re-open it for feedback.

This change would involve the test method being passed to all test instance construction related extensions, TestInstanceFactory, TestInstancePreConstructCallback and TestInstancePreDestroyCallback.

An example description of why this would be helpful is here: quarkusio/quarkus#38336.

The summary is that some frameworks (like Quarkus) allow for the CDI container to be configured by the test method when the test lifecycle is PER_METHOD, like to override configuration properties for a specific test case, before initializing the container. This allows for beans customized to a specific test case to be injected into the class, with all the appropriate configurations already applied.

This is possible with a fully mutable test class where Quarkus can use BeforeEach and AfterEach to set up / tear down the container before the tests run and ensure all beans are injected, but that breaks down when all fields in the class are final.
Just as an example, I usually set lombok.config settings lombok.fieldDefaults.defaultFinal and lombok.fieldDefaults.defaultPrivate to true, so all fields are private and final by default in the entire project, including all tests, which then means I have to disable that by putting @FieldDefaults(makeFinal = false) on each test class using that functionality, which is a bit inconvenient (and keeping fields mutable isn't my preference).

If the test method was passed to the test instance factory extensions, then this would allow for a CDI container to be configured on a per test basis while at the same time maintaining an immutable class.

Deliverables

  • TestInstanceFactory#createTestInstance should receive the test method through ExtensionContext#getTestMethod
  • TestInstancePreConstructCallback#preConstructTestInstance should receive the test method through ExtensionContext#getTestMethod
  • TestInstancePreDestroyCallback#preDestroyTestInstance should receive the test method through ExtensionContext#getTestMethod

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions