Test status quo for @⁠Sql in @⁠Nested test class w/ class-level execution phases

This commit is contained in:
Sam Brannen 2023-10-08 18:16:31 +02:00
parent 8ed302bfa4
commit d1b43386b1
2 changed files with 48 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.test.context.jdbc;
import javax.sql.DataSource;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@ -38,6 +39,7 @@ 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;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS;
/**
* Verifies that {@link Sql @Sql} with {@link ExecutionPhase#AFTER_TEST_CLASS}
@ -69,6 +71,26 @@ class AfterTestClassSqlScriptsTests extends AbstractTransactionalTests {
assertUsers("Catbert", "Dogbert");
}
@Nested
@Sql(scripts = "recreate-schema.sql", executionPhase = BEFORE_TEST_CLASS)
@Sql(scripts = "drop-schema.sql", executionPhase = AFTER_TEST_CLASS)
class NestedAfterTestClassSqlScriptsTests {
@Test
@Order(1)
@Sql("data-add-catbert.sql")
void databaseHasBeenInitialized() {
assertUsers("Catbert");
}
@Test
@Order(2)
@Sql("data-add-dogbert.sql")
void databaseIsNotWipedBetweenTests() {
assertUsers("Catbert", "Dogbert");
}
}
static class VerifySchemaDroppedListener extends AbstractTestExecutionListener {

View File

@ -16,6 +16,7 @@
package org.springframework.test.context.jdbc;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.test.annotation.DirtiesContext;
@ -36,7 +37,7 @@ import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.OVERR
*/
@SpringJUnitConfig(EmptyDatabaseConfig.class)
@DirtiesContext
@Sql(scripts = {"schema.sql", "data-add-catbert.sql"}, executionPhase = BEFORE_TEST_CLASS)
@Sql(scripts = {"recreate-schema.sql", "data-add-catbert.sql"}, executionPhase = BEFORE_TEST_CLASS)
class BeforeTestClassSqlScriptsTests extends AbstractTransactionalTests {
@Test
@ -58,4 +59,28 @@ class BeforeTestClassSqlScriptsTests extends AbstractTransactionalTests {
assertUsers("Catbert", "Dogbert");
}
@Nested
class NestedBeforeTestClassSqlScriptsTests {
@Test
void classLevelScriptsHaveBeenRun() {
assertUsers("Catbert");
}
@Test
@Sql("data-add-dogbert.sql")
@SqlMergeMode(MERGE)
void mergeDoesNotAffectClassLevelPhase() {
assertUsers("Catbert", "Dogbert");
}
@Test
@Sql({"data-add-dogbert.sql"})
@SqlMergeMode(OVERRIDE)
void overrideDoesNotAffectClassLevelPhase() {
assertUsers("Catbert", "Dogbert");
}
}
}