@ -27,23 +27,30 @@ import org.junit.runner.RunWith;
@@ -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
@@ -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 ) ;
}
}