InstancePostProcessor not invoked when extension is registered on test method #603

Closed
slu-it opened this Issue Dec 14, 2016 · 5 comments

Projects

None yet

4 participants

@slu-it
slu-it commented Dec 14, 2016 edited

I was testing JUnit 5's lifecycle in regards to the different extension types for a blog I wrote. In doing so I found out that TestInstancePostProcessor extensions are not invoked when declared on a test method like this:

public class SomeTest {
  @Test
  @ExtendsWith(MyTestInstancePostProcessor.class)
  void testSomething(){
    ..
  }
}

Was this an oversight, or a deliberate decision? On the one hand I find it hard to formulate a viable usecase for having a special post processor just for a single method. But on the other hand: Every method is invoked on its own 'personal' test class instance - so why not allow it?

The code I used to test this is available here on GitHub, specifically the TestsWithExtensionsOnOneMethod test class.

@marcphilipp
Member
marcphilipp commented Dec 14, 2016 edited

It was more of a forward-looking design decision because we're planning on introducing "scenario tests" (see #48) where not every test method would have its own personal test class instance.

However, we could still consider taking TestInstancePostProcessors registered on methods into account for regular tests. I guess we would then have to throw an exception if someone tries to do that in a scenario test.

@junit-team/junit-lambda What do you think?

@nicolaiparlog
Contributor

I think a lot speaks for including the extension point:

  • unit tests will likely be the much more common case so I'd say it has prerogative
  • that the extension is not included in unit tests is more surprising than that it is not included in scenario test
  • throwing an exception is much more explicit than ignoring an extension

I consider the second point to be the strongest. A user can deduce why scenario tests can have no such extensions just by understanding scenario tests and the extension model. If you just know about unit tests and the extension model, you would assume that the extension point is active. To understand why it is not, you need to know about a third thing, namely scenario tests, which are otherwise pretty unrelated.

@sbrannen
Member

However, we could still consider taking TestInstancePostProcessors registered on methods into account for regular tests. I guess we would then have to throw an exception if someone tries to do that in a scenario test.

If we throw an exception in cases such as scenario tests, I can live with supporting TestInstancePostProcessor at the test method level.

Please keep in mind that the switch that turns on "test instance per class" semantics will almost certainly be a generic feature that anyone can enable whenever he or she wishes (i.e., like it was in the JUnit Lambda prototype).

@marcphilipp marcphilipp modified the milestone: 5.0 M4, 5.0 M5 Jan 8, 2017
@marcphilipp
Member

I have assigned this issue to M4 since it's required to inject parameters into fields which is a common use case for parameterized tests, e.g. when providing migration support for the Parameterized runner.

@marcphilipp marcphilipp self-assigned this Jan 8, 2017
@marcphilipp
Member

in progress

@sbrannen sbrannen changed the title from InstancePostProcessor not invoked when extension is defined on test method to InstancePostProcessor not invoked when extension is registered on test method Jan 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment