2

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.

Hakanai
  • 12,010
  • 10
  • 62
  • 132

1 Answers1

0

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

  • 5.2.2 is literally the section which contains the quote that I included in the first paragraph of the question so one might assume that I have read it. – Hakanai Aug 18 '21 at 00:19