0

I'm using JUnit 5.3.0-RC1, trying to implement an extension for the new TestInstanceFactory interface. (I want to get the objects from Weld for CDI injection etc.)

If I implement TestInstanceFactory I can create my own instances. If I also implement AfterEachCallback I can clean them up (shut down Weld) after each test. So far, so good.

However: for this I have to assume TestInstance.LifeCycle.PER_METHOD. If the test class uses TestInstance.Lifecycle.PER_CLASS I would have to detect this and wait until AfterAllCallback before destroying Weld. But ExtensionContext.getTestInstanceLifecycle is declared as Optional, so I may not be able to tell which Lifecycle is active.

Is there a way of having JUnit simply tell me when to destroy the instance? It would be nice to have an extension like TestInstanceDestructor, which would be called whenever JUnit has finished using the test instance.

Secondly, AfterEachCallback is never called for @Disabled tests, which means I can't clean up the instances which were created through my TestInstanceFactory. Starting Weld is kind of expensive, so ideally I'd skip doing that work for disabled tests anyway. Is there some way I can tell which instances are being created for disabled tests?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
seanf
  • 6,504
  • 3
  • 42
  • 52

1 Answers1

1

But ExtensionContext.getTestInstanceLifecycle is declared as Optional, so I may not be able to tell which Lifecycle is active.

As discussed in the JUnit 5 Issue Tracker, you can reliably detect which Lifecycle mode is active.

Is there a way of having JUnit simply tell me when to destroy the instance?

You previous proposal implementing AfterEachCallback or AfterAllCallback based on the current Lifecycle should suffice.

But keep in mind that you can always store a CloseableResource in the ExtensionContext.Store if you want to clean up something after the current context is closed. You can also opt to store such an object in the parent Store if your extension is currently executing at the method level.

Is there some way I can tell which instances are being created for disabled tests?

No, I don't think that is possible. A TestInstanceFactory never knows anything about the method about to be executed, since a TestInstanceFactory is invoked at the class level.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • Thanks Sam. Especially for the tip about the Store. Regarding disabled tests, if [this feature](https://github.com/junit-team/junit5/issues/1568) were to be implemented, would it cover the case where I want to skip expensive test instance construction for tests which are disabled? I could check for the `@Disabled` annotation on the Method object, but in view of other ExecutionConditions I suspect I wouldn't have everything I need. Does that sound right? – seanf Sep 01 '18 at 14:24
  • The only way to know if a test method is _disabled_ is to allow the framework to actually evaluate all registered `ExecutionCondition` extensions. So I don't see how one could _know_ the outcome in advance, but perhaps I'm overlooking something. – Sam Brannen Sep 01 '18 at 14:28