diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java index 1bb5ad5032c..f77ed9bc28b 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/TransactionalSqlScriptsTests.java @@ -17,7 +17,6 @@ package org.springframework.test.context.jdbc; import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; @@ -31,21 +30,19 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; * @since 4.1 */ @SpringJUnitConfig(EmptyDatabaseConfig.class) -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@TestMethodOrder(MethodOrderer.Alphanumeric.class) @Sql({ "schema.sql", "data.sql" }) @DirtiesContext -public class TransactionalSqlScriptsTests extends AbstractTransactionalTests { +class TransactionalSqlScriptsTests extends AbstractTransactionalTests { @Test - @Order(1) - public void classLevelScripts() { + void classLevelScripts() { assertNumUsers(1); } @Test @Sql({ "recreate-schema.sql", "data.sql", "data-add-dogbert.sql" }) - @Order(2) - public void methodLevelScripts() { + void methodLevelScripts() { assertNumUsers(2); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java index 8132c4e9aee..b3b1894464e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/rules/TransactionalSqlScriptsSpringRuleTests.java @@ -27,23 +27,30 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.jdbc.EmptyDatabaseConfig; import org.springframework.test.context.jdbc.Sql; -import org.springframework.test.context.jdbc.TransactionalSqlScriptsTests; +import org.springframework.test.jdbc.JdbcTestUtils; + +import static org.assertj.core.api.Assertions.assertThat; /** - * This class is an extension of {@link TransactionalSqlScriptsTests} - * that has been modified to use {@link SpringClassRule} and - * {@link SpringMethodRule}. + * This class is a JUnit 4 based copy of + * {@link org.springframework.test.context.jdbc.TransactionalSqlScriptsTests} + * that has been modified to use {@link SpringClassRule} and {@link SpringMethodRule}. * * @author Sam Brannen * @since 4.2 */ @RunWith(JUnit4.class) -// Note: @FixMethodOrder is NOT @Inherited. +@ContextConfiguration(classes = EmptyDatabaseConfig.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) -// Overriding @Sql declaration to reference scripts using relative path. @Sql({ "../../jdbc/schema.sql", "../../jdbc/data.sql" }) -public class TransactionalSqlScriptsSpringRuleTests extends TransactionalSqlScriptsTests { +@DirtiesContext +public class TransactionalSqlScriptsSpringRuleTests { @ClassRule public static final SpringClassRule springClassRule = new SpringClassRule(); @@ -54,24 +61,27 @@ public class TransactionalSqlScriptsSpringRuleTests extends TransactionalSqlScri @Rule public Timeout timeout = Timeout.builder().withTimeout(10, TimeUnit.SECONDS).build(); + @Autowired + JdbcTemplate jdbcTemplate; + - /** - * Redeclared to ensure that {@code @FixMethodOrder} is properly applied. - */ @Test - @Override public void classLevelScripts() { assertNumUsers(1); } - /** - * Overriding {@code @Sql} declaration to reference scripts using relative path. - */ @Test @Sql({ "../../jdbc/drop-schema.sql", "../../jdbc/schema.sql", "../../jdbc/data.sql", "../../jdbc/data-add-dogbert.sql" }) - @Override public void methodLevelScripts() { assertNumUsers(2); } + private void assertNumUsers(int expected) { + assertThat(countRowsInTable("user")).as("Number of rows in the 'user' table.").isEqualTo(expected); + } + + private int countRowsInTable(String tableName) { + return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName); + } + }