Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested PER_METHOD inside PER_CLASS should call TestInstanceFactory for enclosing class #1566

Closed
aschoerk opened this issue Aug 29, 2018 · 7 comments

Comments

@aschoerk
Copy link

aschoerk commented Aug 29, 2018

Currently I am trying to integrate JUnit 5.3.0-RC1 into a Weld-SE-Test framework, ejb-cdi-unit.

The first solution based on JUnit 5.2.0 available in ejb-cdi-unit:1.1.15 restricts the TestInstance-Classes somewhat concerning CDI. I can only allow @Inject. That helps already very much, but see this note.

Now while trying to implement the TestInstanceFactory to avoid the aforementioned restrictions, I encounter problems with this example.

You can see a ServiceTest5 test class working with lifecycle PER_CLASS and two nested test classes of which one is working with lifecycle PER_METHOD.

The inner classes are currently disabled to avoid email notifications from Travis CI.

I would expect, that before the method ServiceTest5.WhenSomeEntitiesInDbExist.cantReadTestDataUsingServiceInRequiredNew() is called, that a completely new instance of ServiceTest5 would be created using the TestInstanceFactory implemented in JUnit5Extension, since the nested inner class WhenSomeEntitiesInDbExist has Lifecycle PER_METHOD.

That is not the case.

Is that by design or a bug in the release candidate?

@sbrannen
Copy link
Member

Thanks for raising this issue before 5.3 GA!

I am not yet certain if it's a "bug"; however, I am going to label it as a bug for the time being to ensure that it gets looked at with high priority.

@sbrannen
Copy link
Member

sbrannen commented Aug 29, 2018

OK. I took at look at the code in question.

This is the expected behavior.

A @Nested test class cannot change the @TestInstance lifecycle mode declared for an enclosing class.

Thus, in your example, ServiceTest5 retains its declared PER_CLASS lifecycle mode regardless of any declarations for a different lifecycle mode on a nested test class.

Please note that this behavior is specific to the semantics for @TestInstance lifecycle and therefore not directly related to the new TestInstanceFactory extension API.

I am therefore closing this issue as "works as designed".

@sbrannen sbrannen changed the title Nested PER_METHOD inside PER_CLASS should call TestInstanceFactory Nested PER_METHOD inside PER_CLASS should call TestInstanceFactory for enclosing class Aug 29, 2018
@sbrannen sbrannen removed this from the 5.3 GA milestone Aug 29, 2018
@aschoerk
Copy link
Author

Thank you, but that is unexpected, so inner classes may not override the Lifecycle, or only if the outer is PER CLASS? Shouldn't this lead to an error, since the results are unexpected?

@sbrannen
Copy link
Member

I suppose whether it's expected behavior or not is debatable, but that is the behavior the JUnit Team decided on when the feature was implemented.

A @Nested test class can be configured with its own lifecycle mode which may differ from that of an enclosing test class.

A @Nested test class cannot change the lifecycle mode of an enclosing test class.

The general rule is that each test class (whether top-level, nested, or a subclass) decides for itself what its lifecycle mode should be. The only form of inheritance with regard to lifecycle modes is that subclasses inherit the mode from their parent class by default.

@aschoerk
Copy link
Author

aschoerk commented Aug 29, 2018

Just to clarify:
" JUnit creates a new instance of each test class before executing each test method" in case of PER_METHOD in inner classes means that the current instance of the inner class is recreated every call of a method, but in case of outer-class "PER_CLASS" not the instance of the outer class. The creation of inner classes are not done via the TestInstanceFactory.

Btw. Thank you very much for the quick responses.

@seanf
Copy link
Contributor

seanf commented Aug 30, 2018

@aschoerk said:

The creation of inner classes are not done via the TestInstanceFactory.

They can be, because JUnit 5 has a unit test which shows it. The problem is when the same TestInstanceFactory is used on the outer and inner classes: #1567

@sbrannen
Copy link
Member

Just to clarify: " JUnit creates a new instance of each test class before executing each test method" in case of PER_METHOD in inner classes means that the current instance of the inner class is recreated every call of a method, but in case of outer-class "PER_CLASS" not the instance of the outer class.

That's correct.

The creation of inner classes are not done via the TestInstanceFactory.

They should be if there is a TestInstanceFactory registered for the @Nested test class.

Btw. Thank you very much for the quick responses.

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants