-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Allow @BeforeAll and @AfterAll methods to be non-static #419
Comments
The short answer is "no". For further details, see #48, #88, and especially this comment: #88 (comment). However, I will leave this issue open since the question is asked so frequently on its own unrelated to scenario tests. |
FYI: I have updated this issue's title and added an Overview section to its description. |
I see. Thanks! |
@sbrannen Allowing As described here, currently we have to create a Enabling that would allow Junit 5 usage much more simple, with idiomatic Kotlin code without requiring usage of a Kotlin specific library for testing. |
Hi @sdeleuze, Nice to see a fellow core Spring committer chiming in here! 😉 As for your request... you're preaching to the choir (at least when addressing me). In the JUnit Lambda prototype @bechte and I implemented support for I am personally totally in favor of bringing back that support. In fact, it is simply a requirement for proper support for Scenario Tests (see #48 for details). So it will have to be pulled back into the framework in one form or other, and that should happen in the M5 time frame. On a different topic, what do you think about using Kotlin's spek testing framework? Is that a viable option for you? Or are you more focused on using the Spring TestContext Framework with JUnit Jupiter support for testing your Kotlin codebase? |
@junit-team/junit-lambda what do you guys think about reinstating the |
My pleasure to be here :-) I think both could make sense depending on the use case. For functional Spring + Kotlin applications like the one I develop on https://github.com/mix-it/mixit, I think Spek could be a better fit (we are going to use it shortly, it is on our TODO), while for regular Spring Boot annotation based applications, being able to take advantage of Spring TestContext Framework with JUnit Jupiter support would make sense for a lot of developers I think. |
Slated for M4 now in order to ease adoption for Kotlin users. |
Disclaimer: slated for M4, but might get shifted forward to M5 again. 😉 |
That would be awesome in M4, but I notice the disclaimer ;-) Kotlin support is now one of the major themes of Spring Framework 5 as announced in my recent blog post and this issue is the only really annoying issue with Kotlin + Junit 5 I am aware of, so fingers crossed ;-) |
I'm ok with reinstating it. We need to make sure to validate that test methods don't add instance level extensions like |
I'm not sure about that: sometimes you may want dependencies to be re-injected between test methods. So it's really up to the extension. For example, with TestNG (which supports scenario-style tests), the Spring TestContext Framework re-injects dependencies if the previously executed test method was annotated with |
Point taken. |
FYI: I have added Proposal and Open Issues sections to this issue's description. |
Prior to this set of commits, the lifecycle for a test instance followed per-method semantics, meaning that a new test instance was created before the execution of each test, test factory, etc. This behavior had a few drawbacks in certain scenarios. For example, it was impossible to declare @BeforeAll and @afterall on non-static methods since there was no cached instance against which those methods could be invoked. It also made it more difficult to implement @BeforeAll and @afterall methods in programming languages such as Kotlin. This set of commits addresses these issues by reintroducing the @testinstance annotation from the JUnit Lambda prototype. @testinstance allows the test instance lifecycle to switched from the default per-method mode to a new per-class mode simply by annotating the test class with @testinstance(Lifecycle.PER_CLASS). This enables shared test instance state between test methods in a given test class as well as between non-static @BeforeAll and @afterall methods in a test class. Furthermore, since @BeforeAll and @afterall methods are no longer required to be static (if the test class is annotated with @testinstance(Lifecycle.PER_CLASS)), the following new use cases are now supported. - Declaration of @BeforeAll and @afterall methods in @nested test classes. - Declaration of @BeforeAll and @afterall on interface default methods. - Simplified declaration of @BeforeAll and @afterall methods in test classes implemented with the Kotlin programming language. As a logical side effect of these changes, the getTestInstance() method in the TestExtensionContext API has been moved to the ExtensionContext API and now returns Optional<Object> instead of simply Object. This makes it possible to access the test instance from container-level extension APIs such as BeforeAllCallback and AfterAllCallback when tests are executed using the per-class lifecycle mode. Issue: #419
This issue has been addressed in |
Sorry to be late to the discussion. One option that I have considered in the past is to allow Since extensions have access to the (yes I realize for Kotlin we wouldn't want to require static methods; just suggesting a different solution for the general problem of scenario tests). |
Yesss... rather late to the closed discussion. 😉 If you'd like the team to consider your idea about sharing the Cheers! |
Overview
#88 explains why
@BeforeAll
and@AfterAll
methods currently must bestatic
; however, the question arises again and again.Related Issues
Proposal
Reintroduce support for
@TestInstance
as was present in the JUnit 5 Prototype, thereby allowing developers and third party extension authors to make use of the feature as well. In other words, we do not want to limit use of this feature to JUnit Jupiter itself.Open Issues
Special caution must be taken with regard to
@Nested
test classes in terms of the lifecycle of the test instance against which@BeforeAll
and@AfterAll
methods are executed.Original Issue Description
Can support be added for
@BeforeAll
and@AfterAll
methods in@Nested
test classes?For example:
Deliverables
@TestInstance
as was present in the JUnit 5 prototype.@TestInstance
an@Inherited
annotation to support lifecycle inheritance semantics within a test class hierarchy.@TestInstance
for@Nested
test classes.@Nested
test classes in terms of the lifecycle of the test instance against which@BeforeAll
and@AfterAll
methods are executed.@BeforeAll
and@AfterAll
methods to be non-static.getTestInstance()
method fromTestExtensionContext
toExtensionContext
.ExtensionContext
.TestInstancePostProcessor
extensions via theExtensionContext
as well.TestTemplateContainerExtensionContext
extensions via theExtensionContext
as well.@BeforeAll
and@AfterAll
methods must bestatic
.@BeforeAll
and@AfterAll
methods must bestatic
.The text was updated successfully, but these errors were encountered: