Skip to content

Introduce MethodInvoker API for TestExecutionListeners #31199

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

Closed
sbrannen opened this issue Sep 10, 2023 · 0 comments
Closed

Introduce MethodInvoker API for TestExecutionListeners #31199

sbrannen opened this issue Sep 10, 2023 · 0 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Sep 10, 2023

Overview

In order to be able to support parameter injection in @BeforeTransaction and @AfterTransaction methods (see #30736), we need a generic mechanism for delegating to the underlying testing framework to invoke methods.

While we're at it, we could do the same for constructors, but I'm hesitant to add a feature that TestExecutionListener authors will never actually need.

In light of that, we should introduce an MethodInvoker API for TestExecutionListeners in the Spring TestContext Framework.

The default implementation should simply invoke the method without arguments.

A JUnit Jupiter specific implementation (registered in the SpringExtension) should delegate to the org.junit.jupiter.api.extension.ExtensionContext.getExecutableInvoker() mechanism introduced in JUnit Jupiter 5.9. This will allow 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.

Related Issues

@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement labels Sep 10, 2023
@sbrannen sbrannen added this to the 6.1.0-RC1 milestone Sep 10, 2023
@sbrannen sbrannen self-assigned this Sep 10, 2023
@sbrannen sbrannen changed the title Introduce ExecutableInvoker API for TestExecutionListeners Introduce MethodInvoker API for TestExecutionListeners Sep 10, 2023
sbrannen added a commit that referenced this issue Sep 10, 2023
Prior to this commit, @​BeforeTransaction and @​AfterTransaction
methods could not accept any arguments. This is acceptable with testing
frameworks such as JUnit 4 and TestNG that do not provide support for
parameter injection. However, users of JUnit Jupiter have become
accustomed to being able to accept arguments in lifecycle methods
annotated with JUnit's @​BeforeEach, @​AfterEach, etc.

As a follow up to the previous commit (see gh-31199), this commit
introduces first-class support for parameter injection in
@​BeforeTransaction and @​AfterTransaction methods, as demonstrated in
the following example.

@​BeforeTransaction
void verifyInitialDatabaseState(@Autowired DataSource dataSource) {
	// Use the DataSource to verify the initial DB state
}

Closes gh-30736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant