javajunit5junit5-extension-model

Can JUnit 5 actually inject extension instances?


The documentation for @RegisterExtension mentions something about injection of the instance:

the extension will be registered after the test class has been instantiated and after all TestInstancePostProcessors have been given a chance to post-process the test instance (potentially injecting the instance of the extension to be used into the annotated field).

Are there any post-processors in JUnit which can do this for extensions automatically, or is this just talking about things people might build in the future?

In our case, I found that many of our extensions turn out to be like this:

    @RegisterExtension
    public final TempFolderExtension temp = new TempFolderExtension();

    @RegisterExtension
    public final SomeFactoryExtension factory = new SomeFactoryExtension(temp);

A container like PicoContainer could automatically figure out how to construct the instances.

It would be really nifty if we could just write,

    @RegisterExtension
    public TempFolderExtension temp;

    @RegisterExtension
    public SomeFactoryExtension factory;

And either an annotation processor or a runtime injector could fill in the rest.

Dependencies between this sort of extension also has implications for the order they should run in.


Solution

  • Short answer: No, it is not possible, when using @RegisterExtension you have to construct your extension instance manually.

    Read https://junit.org/junit5/docs/current/user-guide/#extensions 5.2.2