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

Introduce class-level execution phases for @Sql #27285

Closed
wants to merge 4 commits into from

Conversation

aahlenst
Copy link
Contributor

This PR is a proposal to address #18929.

So far, scripts or inline statements declared with @Sql run before or after every test method. It is not possible to declare a script or inline statement to be run once before any test method has been run or after all test methods have been run.

This PR makes it possible by adding two additional execution phases, BEFORE_TEST_CLASS and AFTER_TEST_CLASS. Scripts or inline statements with BEFORE_TEST_CLASS will be run once before any test method has been started. Anything marked with AFTER_TEST_CLASS will be run after all test methods have been completed.

#1835 added MergeMode. MergeMode allows method-level @Sql annotations to override or extend class-level @Sql annotations. In my proposal, MergeMode has no effect on @Sql annotations with an execution phase of BEFORE_TEST_CLASS or AFTER_TEST_CLASS. The reason is that any method-level processing would come too late to influence a script or inline statement that runs when the test context is being set up. Furthermore, it would pose the question how to resolve conflicting annotations. If one test method wished to override a class-level BEFORE_TEST_CLASS annotation while others would not, honoring that wish would effectively turn BEFORE_TEST_CLASS into BEFORE_TEST_METHOD. AFTER_TEST_CLASS has roughly the same problem, just backwards.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 16, 2021
@rstoyanchev rstoyanchev added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Nov 10, 2021
@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement labels Sep 27, 2022
Copy link
Member

@snicoll snicoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I've added a couple comments for your consideration.

@sbrannen sbrannen linked an issue Sep 16, 2023 that may be closed by this pull request
@sbrannen sbrannen changed the title Add class-level execution phases for @Sql Introduce class-level execution phases for @Sql Sep 16, 2023
Copy link
Member

@sbrannen sbrannen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR looks pretty good.

Please revert changes that deleted intentional blank lines, and please rebase on main.

Thanks!

@sbrannen sbrannen added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 16, 2023
@sbrannen sbrannen self-assigned this Sep 16, 2023
@sbrannen sbrannen added this to the 6.1.0-RC1 milestone Sep 16, 2023
@sbrannen
Copy link
Member

Please revert changes that deleted intentional blank lines, and please rebase on main.

FYI: we would like to include this feature in the upcoming Spring Framework 6.1 RC1.

So if you don't have time to rebase on main, just let us know, and we'll take over the PR as-is.

@aahlenst
Copy link
Contributor Author

FYI: we would like to include this feature in the upcoming Spring Framework 6.1 RC1.

That's awesome. Thanks a lot.

I have updated the PR. Due to its age, it required some rework. I also improved a test. If I should do anything else, like adding the exception as @snicoll suggested, let me know.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 17, 2023
@sbrannen
Copy link
Member

I have updated the PR.

Thanks!

Due to its age, it required some rework.

I can well imagine.

I also improved a test.

OK, I'll check it out in the coming week.

If I should do anything else, like adding the exception as @snicoll suggested, let me know.

Yes, please throw an IllegalArgumentException -- can be achieved via Spring's Assert class as in other places in the TestContext framework.

@sbrannen sbrannen added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Sep 17, 2023
Copy link
Member

@sbrannen sbrannen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is almost ready, though I've requested a few additional minor changes.

@aahlenst
Copy link
Contributor Author

aahlenst commented Oct 1, 2023

@sbrannen I applied the requested changes.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 1, 2023
@sbrannen sbrannen removed the status: feedback-provided Feedback has been provided label Oct 1, 2023
sbrannen pushed a commit that referenced this pull request Oct 4, 2023
This commit introduces BEFORE_TEST_CLASS and AFTER_TEST_CLASS execution
phases for @⁠Sql.

See gh-27285
@sbrannen sbrannen closed this in 0afcb4d Oct 4, 2023
@sbrannen
Copy link
Member

sbrannen commented Oct 4, 2023

This has been merged into main in 5aa2d05 and revised in 0afcb4d.

It turns out that hasTestMethod() wasn't needed after all. See the commit message for the second commit for details on what I changed.

Thanks for the contribution! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce class-level execution phases for @Sql [SPR-14357]
5 participants