-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support parameter injection in @BeforeTransaction
/@AfterTransaction
methods
#30736
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
Comments
@BeforeTransaction
/`@AfterTransaction methods
@BeforeTransaction
/`@AfterTransaction methods@BeforeTransaction
/@AfterTransaction
methods
The reason is that JUnit Jupiter invokes JUnit lifecycle methods such as Whereas, Spring invokes Fortunately, it should be possible for Spring to delegate to JUnit Jupiter to invoke Of course, that means this functionality would only be available to JUnit Jupiter tests (and not to JUnit 4 or TestNG tests). So we will have to introduce a Spring-specific abstraction over the I'll see what's possible... |
Thanks for the feedback, if is not possible, the current reference documentation should be updated to indicate this "constraint". |
FWIW, I actually documented this in JUnit 5's issue tracker quite a long time ago. 😉 |
Thanks for the link, but I meant about Spring Framework Reference documentation. Yes, it is tricky: in what documentation should be located this case. For JUnit or SF?, because Autowire and Transactions is involved I expected to see this in the SF Reference documentation. I hope you see my point |
In order to be able to support parameter injection in @BeforeTransaction and @AfterTransaction methods (see gh-30736), this commit introduces a MethodInvoker API for TestExecutionListeners as a generic mechanism for delegating to the underlying testing framework to invoke methods. The default implementation simply invokes the method without arguments, which allows TestExecutionListeners using this mechanism to operate correctly when the underlying testing framework is JUnit 4, TestNG, etc. A JUnit Jupiter specific implementation is registered in the SpringExtension which delegates to the ExtensionContext.getExecutableInvoker() mechanism introduced in JUnit Jupiter 5.9. This allows a TestExecutionListener to transparently benefit from registered ParameterResolvers in JUnit Jupiter (including the SpringExtension) when invoking user methods, effectively providing support for parameter injection for arbitrary methods. Closes gh-31199
This has been addressed in ed83461. Feel free to try it out in 6.1 M5 snapshot builds. |
Uh oh!
There was an error while loading. Please reload this page.
For Spring Framework 6.0.4 --- regarding testing with JUnit Jupiter -- the following works fine.
The point is that an
@AfterEach
method accepts injection through parameters at runtime.Same as
@BeforeAll
, I am assuming@BeforeEach
works as well.Now the following:
That compiles, but at runtime we get:
Questions
What is the reason for this?
I am assuming that
@BeforeTransaction
has the same situation. Currently as a workaround I use:Note the injected
@Service
classes are@Transactional
The text was updated successfully, but these errors were encountered: