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

Improve documentation for @Sql execution phases regarding lifecycle #32343

Closed
marwin1991 opened this issue Feb 28, 2024 · 5 comments
Closed

Improve documentation for @Sql execution phases regarding lifecycle #32343

marwin1991 opened this issue Feb 28, 2024 · 5 comments
Assignees
Labels
in: test Issues in the test module type: documentation A documentation task
Milestone

Comments

@marwin1991
Copy link

marwin1991 commented Feb 28, 2024

Affects: spring-test 6.1.4


There is a bug, when using @Sql with BEFORE_TEST_CLASS.

Without this annotation the order of execution of methods is:

  • method annotated with @BeforeAll
  • method annotated with @DynamicPropertySource
  • starting spring
    (often used to start some mock, then pass it to properties)

BUT when @Sql with BEFORE_TEST_CLASS is used, the order is:

  • method annotated with @DynamicPropertySource
  • starting spring
  • method annotated with @BeforeAll

I have created sample repo: https://github.com/marwin1991/sql-before-class-bug-reproduction

with tests:

Second test not passing, because the order IMO is incorrect: it should not change after adding @Sql.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 28, 2024
@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Feb 28, 2024
@sbrannen sbrannen self-assigned this Feb 28, 2024
@sbrannen sbrannen removed the type: bug A general bug label Mar 1, 2024
@sbrannen
Copy link
Member

sbrannen commented Mar 1, 2024

Hi @marwin1991,

Congratulations on submitting your first issue for the Spring Framework! 👍

The behavior you have described is not a bug but rather the expected behavior.

Spring cannot execute SQL scripts without access to the DataSource/PlatformTransactionManager from your ApplicationContext.

When you use the BEFORE_TEST_CLASS mode with @Sql, the SqlScriptsTestExecutionListener has to load the ApplicationContext via the TestExecutionListener#beforeTestClass callback, which is invoked before JUnit Jupiter @BeforeAll lifecycle methods.

Since @DynamicPropertySource methods are invoked when loading the ApplicationContext, that means @DynamicPropertySource methods are invoked before @BeforeAll methods when using the BEFORE_TEST_CLASS mode.

In other words, the invocation order of those methods is required to change when using the BEFORE_TEST_CLASS mode.

Have you considered converting your @BeforeAll method to a static initialization block as follows?

static {
    System.out.println("Calling init method");

    var = 1;

    // init here mock server f.e
    // mockWebServer = new MockWebServer();
    // mockWebServer.start();
}

@sbrannen sbrannen changed the title When using @Sql annotation in tests, @DynamicPropertySource is executed before method @BeforeAll When using @Sql with BEFORE_TEST_CLASS, @DynamicPropertySource is executed before @BeforeAll method Mar 1, 2024
@sbrannen
Copy link
Member

sbrannen commented Mar 1, 2024

The Javadoc for ExecutionPhase.BEFORE_TEST_CLASS currently states the following.

The configured SQL scripts and statements will be executed once per test class before any test method is run.

However, that is a bit misleading since one can infer that the SQL scripts and statements may be executed after "before class level" callbacks, which is not the case.

In light of that, I am repurposing this issue to improve the documentation.

@sbrannen sbrannen added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 1, 2024
@sbrannen sbrannen added this to the 6.1.5 milestone Mar 1, 2024
@sbrannen sbrannen changed the title When using @Sql with BEFORE_TEST_CLASS, @DynamicPropertySource is executed before @BeforeAll method Improve documentation for @Sql's BEFORE_TEST_CLASS mode regarding lifecycle Mar 1, 2024
@marwin1991
Copy link
Author

@sbrannen

Thank you for your clarification ❤️

I was thinking the order of sql BEFORE_TEST_CLASS is as follows:

  • method annotated with @BeforeAll
  • method annotated with @DynamicPropertySource
  • Spring is init (datasources and application context also)
  • SQL is executed
  • first test is executed

@sbrannen sbrannen changed the title Improve documentation for @Sql's BEFORE_TEST_CLASS mode regarding lifecycle Improve documentation for @Sql execution phases regarding lifecycle Mar 1, 2024
@sbrannen
Copy link
Member

sbrannen commented Mar 1, 2024

@marwin1991, you can see the updated documentation here:

https://docs.spring.io/spring-framework/docs/6.1.5-SNAPSHOT/javadoc-api/org/springframework/test/context/jdbc/Sql.ExecutionPhase.html#BEFORE_TEST_CLASS

@marwin1991
Copy link
Author

@sbrannen 👍🏼

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: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants