Revise "Introduce class-level execution phases for @⁠Sql"

This commit revises the previous commit as follows.

- Remove hasTestMethod() from TestContext and instead introduce a new
  variant of createDelegatingTransactionAttribute() in
  TestContextTransactionUtils which accepts a boolean
  `includeMethodName` flag.

- Add missing @⁠TestMethodOrder declaration to
  AfterTestClassSqlScriptsTests to ensure that the test methods are
  always executed in the required order.

- Polish Javadoc and add missing @⁠since tags.

Closes gh-27285
This commit is contained in:
Sam Brannen
2023-10-04 16:36:27 +02:00
parent 5aa2d0515e
commit 0afcb4dfed
8 changed files with 96 additions and 89 deletions

View File

@@ -18,55 +18,66 @@ package org.springframework.test.context.jdbc;
import javax.sql.DataSource;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.core.Ordered;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Commit;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.jdbc.AfterTestClassSqlScriptsTests.VerifySchemaDroppedListener;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.transaction.TestContextTransactionUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_CLASS;
/**
* Verifies that {@link Sql @Sql} with {@link Sql.ExecutionPhase#AFTER_TEST_CLASS} is run after all tests in the class
* have been run.
* Verifies that {@link Sql @Sql} with {@link ExecutionPhase#AFTER_TEST_CLASS}
* is run after all tests in the class have been run.
*
* @author Andreas Ahlenstorf
* @author Sam Brannen
* @since 6.1
*/
@SpringJUnitConfig(PopulatedSchemaDatabaseConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Sql(value = {"drop-schema.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_CLASS)
@TestExecutionListeners(
value = AfterTestClassSqlScriptsTests.VerifyTestExecutionListener.class,
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@TestMethodOrder(OrderAnnotation.class)
@DirtiesContext
@Sql(scripts = "drop-schema.sql", executionPhase = AFTER_TEST_CLASS)
@Commit
@TestExecutionListeners(listeners = VerifySchemaDroppedListener.class, mergeMode = MERGE_WITH_DEFAULTS)
class AfterTestClassSqlScriptsTests extends AbstractTransactionalTests {
@Test
@Order(1)
@Sql(scripts = "data-add-catbert.sql")
@Commit
@Sql("data-add-catbert.sql")
void databaseHasBeenInitialized() {
assertUsers("Catbert");
}
@Test
@Order(2)
@Sql(scripts = "data-add-dogbert.sql")
@Commit
@Sql("data-add-dogbert.sql")
void databaseIsNotWipedBetweenTests() {
assertUsers("Catbert", "Dogbert");
}
static class VerifyTestExecutionListener implements TestExecutionListener, Ordered {
static class VerifySchemaDroppedListener extends AbstractTestExecutionListener {
@Override
public int getOrder() {
// Must run before DirtiesContextTestExecutionListener. Otherwise, the
// old data source will be removed and replaced with a new one.
return 3001;
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
@@ -74,14 +85,9 @@ class AfterTestClassSqlScriptsTests extends AbstractTransactionalTests {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
assertThatExceptionOfType(BadSqlGrammarException.class)
.isThrownBy(() -> jdbcTemplate.queryForList("SELECT name FROM user", String.class));
}
@Override
public int getOrder() {
// Must run before DirtiesContextTestExecutionListener. Otherwise, the old data source will be removed and
// replaced with a new one.
return 3001;
.isThrownBy(() -> jdbcTemplate.queryForList("SELECT name FROM user", String.class))
.withMessageContaining("user");
}
}
}

View File

@@ -19,21 +19,24 @@ package org.springframework.test.context.jdbc;
import org.junit.jupiter.api.Test;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.OVERRIDE;
/**
* Verifies that {@link Sql @Sql} with {@link Sql.ExecutionPhase#BEFORE_TEST_CLASS} is run before all tests in the class
* have been run.
* Verifies that {@link Sql @Sql} with {@link ExecutionPhase#BEFORE_TEST_CLASS}
* is run before all tests in the class have been run.
*
* @author Andreas Ahlenstorf
* @author Sam Brannen
* @since 6.1
*/
@SpringJUnitConfig(classes = EmptyDatabaseConfig.class)
@SpringJUnitConfig(EmptyDatabaseConfig.class)
@DirtiesContext
@Sql(value = {"schema.sql", "data-add-catbert.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@Sql(scripts = {"schema.sql", "data-add-catbert.sql"}, executionPhase = BEFORE_TEST_CLASS)
class BeforeTestClassSqlScriptsTests extends AbstractTransactionalTests {
@Test
@@ -52,7 +55,7 @@ class BeforeTestClassSqlScriptsTests extends AbstractTransactionalTests {
@Sql({"data-add-dogbert.sql"})
@SqlMergeMode(OVERRIDE)
void overrideDoesNotAffectClassLevelPhase() {
assertUsers("Dogbert", "Catbert");
assertUsers("Catbert", "Dogbert");
}
}

View File

@@ -30,6 +30,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_CLASS;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS;
/**
* Unit tests for {@link SqlScriptsTestExecutionListener}.
@@ -58,7 +60,6 @@ class SqlScriptsTestExecutionListenerTests {
void missingValueAndScriptsAndStatementsAtMethodLevel() throws Exception {
Class<?> clazz = MissingValueAndScriptsAndStatementsAtMethodLevel.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.hasTestMethod()).willReturn(true);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
assertExceptionContains(clazz.getSimpleName() + ".foo" + ".sql");
@@ -109,24 +110,22 @@ class SqlScriptsTestExecutionListenerTests {
void beforeTestClassOnMethod() throws Exception {
Class<?> clazz = ClassLevelExecutionPhaseOnMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.hasTestMethod()).willReturn(true);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("beforeTestClass"));
assertThatIllegalArgumentException()
.isThrownBy(() -> listener.beforeTestMethod(testContext))
.withMessage("BEFORE_TEST_CLASS cannot be used on methods");
.withMessage("@SQL execution phase BEFORE_TEST_CLASS cannot be used on methods");
}
@Test
void afterTestClassOnMethod() throws Exception {
Class<?> clazz = ClassLevelExecutionPhaseOnMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.hasTestMethod()).willReturn(true);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("afterTestClass"));
assertThatIllegalArgumentException()
.isThrownBy(() -> listener.beforeTestMethod(testContext))
.withMessage("AFTER_TEST_CLASS cannot be used on methods");
.withMessage("@SQL execution phase AFTER_TEST_CLASS cannot be used on methods");
}
private void assertExceptionContains(String msg) throws Exception {
@@ -175,12 +174,13 @@ class SqlScriptsTestExecutionListenerTests {
static class ClassLevelExecutionPhaseOnMethod {
@Sql(scripts = "foo.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@Sql(scripts = "foo.sql", executionPhase = BEFORE_TEST_CLASS)
public void beforeTestClass() {
}
@Sql(scripts = "foo.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_CLASS)
@Sql(scripts = "foo.sql", executionPhase = AFTER_TEST_CLASS)
public void afterTestClass() {
}
}
}