Skip to content

Commit

Permalink
Test status quo for @⁠Sql in @⁠Nested test class w/ class-level execu…
Browse files Browse the repository at this point in the history
…tion phases
  • Loading branch information
sbrannen committed Oct 8, 2023
1 parent 8ed302b commit d1b4338
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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");
}

}

}

0 comments on commit d1b4338

Please sign in to comment.