From d1b43386b1bc23f87a3cfd0e5de5a3d6ca23f4cb Mon Sep 17 00:00:00 2001
From: Sam Brannen <sam@sambrannen.com>
Date: Sun, 8 Oct 2023 18:16:31 +0200
Subject: [PATCH] =?UTF-8?q?Test=20status=20quo=20for=20@=E2=81=A0Sql=20in?=
 =?UTF-8?q?=20@=E2=81=A0Nested=20test=20class=20w/=20class-level=20executi?=
 =?UTF-8?q?on=20phases?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../jdbc/AfterTestClassSqlScriptsTests.java   | 22 +++++++++++++++
 .../jdbc/BeforeTestClassSqlScriptsTests.java  | 27 ++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/AfterTestClassSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/AfterTestClassSqlScriptsTests.java
index 394f7d45401f..9bb594d437d6 100644
--- a/spring-test/src/test/java/org/springframework/test/context/jdbc/AfterTestClassSqlScriptsTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/AfterTestClassSqlScriptsTests.java
@@ -19,6 +19,7 @@
 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 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 @@ void databaseIsNotWipedBetweenTests() {
 		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 {
 
diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/BeforeTestClassSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/BeforeTestClassSqlScriptsTests.java
index faded5ce5ab1..aa8be3e5b8c2 100644
--- a/spring-test/src/test/java/org/springframework/test/context/jdbc/BeforeTestClassSqlScriptsTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/BeforeTestClassSqlScriptsTests.java
@@ -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 @@
  */
 @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 @@ void overrideDoesNotAffectClassLevelPhase() {
 		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");
+		}
+
+	}
+
 }