Fix tx annotated tests so that they pass in the build

AbstractTransactionalAnnotatedConfigClassTests is now annotated with
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) so 
that side-effects between tests are avoided.

Re-enabled TransactionalAnnotatedConfigClassWithAtConfigurationTests
and TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.

Also introduced a log4j FileAppender for tests that writes to
"build/spring-test.log".

Issue: SPR-9051
This commit is contained in:
Sam Brannen 2012-05-16 03:08:15 +02:00
parent 01a9dd9772
commit 500a4dd995
4 changed files with 27 additions and 45 deletions

View File

@ -24,22 +24,22 @@ import static org.springframework.test.transaction.TransactionTestUtils.inTransa
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.Employee; import org.springframework.beans.Employee;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
* This set of tests investigates the claims made in * This set of tests (i.e., all concrete subclasses) investigates the claims made in
* <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a> * <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a>
* with regard to transactional tests. * with regard to transactional tests.
* *
@ -48,20 +48,13 @@ import org.springframework.transaction.annotation.Transactional;
* @see org.springframework.test.context.testng.AnnotationConfigTransactionalTestNGSpringContextTests * @see org.springframework.test.context.testng.AnnotationConfigTransactionalTestNGSpringContextTests
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public abstract class AbstractTransactionalAnnotatedConfigClassTests { public abstract class AbstractTransactionalAnnotatedConfigClassTests {
protected static final String JANE = "jane"; protected static final String JANE = "jane";
protected static final String SUE = "sue"; protected static final String SUE = "sue";
protected static final String YODA = "yoda"; protected static final String YODA = "yoda";
protected static final int NUM_TESTS = 2;
protected static final int NUM_TX_TESTS = 1;
private static int numSetUpCalls = 0;
private static int numSetUpCallsInTransaction = 0;
private static int numTearDownCalls = 0;
private static int numTearDownCallsInTransaction = 0;
protected DataSource dataSourceFromTxManager; protected DataSource dataSourceFromTxManager;
protected DataSource dataSourceViaInjection; protected DataSource dataSourceViaInjection;
@ -82,11 +75,11 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
this.jdbcTemplate = new JdbcTemplate(dataSource); this.jdbcTemplate = new JdbcTemplate(dataSource);
} }
protected int countRowsInTable(String tableName) { private int countRowsInTable(String tableName) {
return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName); return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
} }
protected int createPerson(String name) { private int createPerson(String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name); return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
} }
@ -103,22 +96,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
assertEquals("Adding '" + name + "'", 1, createPerson(name)); assertEquals("Adding '" + name + "'", 1, createPerson(name));
} }
@BeforeClass
public static void beforeClass() {
numSetUpCalls = 0;
numSetUpCallsInTransaction = 0;
numTearDownCalls = 0;
numTearDownCallsInTransaction = 0;
}
@AfterClass
public static void afterClass() {
assertEquals("number of calls to setUp().", NUM_TESTS, numSetUpCalls);
assertEquals("number of calls to setUp() within a transaction.", NUM_TX_TESTS, numSetUpCallsInTransaction);
assertEquals("number of calls to tearDown().", NUM_TESTS, numTearDownCalls);
assertEquals("number of calls to tearDown() within a transaction.", NUM_TX_TESTS, numTearDownCallsInTransaction);
}
@Test @Test
public void autowiringFromConfigClass() { public void autowiringFromConfigClass() {
assertNotNull("The employee should have been autowired.", employee); assertNotNull("The employee should have been autowired.", employee);
@ -133,10 +110,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
numSetUpCalls++;
if (inTransaction()) {
numSetUpCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 1 : 0), "before a test method"); assertNumRowsInPersonTable((inTransaction() ? 1 : 0), "before a test method");
} }
@ -151,10 +124,6 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
numTearDownCalls++;
if (inTransaction()) {
numTearDownCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 3 : 0), "after a test method"); assertNumRowsInPersonTable((inTransaction() ? 3 : 0), "after a test method");
} }

View File

@ -21,7 +21,6 @@ import static org.junit.Assert.assertSame;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.springframework.beans.Employee; import org.springframework.beans.Employee;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -38,7 +37,6 @@ import org.springframework.transaction.PlatformTransactionManager;
* @since 3.2 * @since 3.2
* @see TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests * @see TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests
*/ */
@Ignore("Disabled until working within the build")
@ContextConfiguration @ContextConfiguration
public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
AbstractTransactionalAnnotatedConfigClassTests { AbstractTransactionalAnnotatedConfigClassTests {
@ -70,6 +68,8 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
public DataSource dataSource() { public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()// return new EmbeddedDatabaseBuilder()//
.addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")// .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build(); .build();
} }
@ -78,7 +78,7 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
@Before @Before
public void compareDataSources() throws Exception { public void compareDataSources() throws Exception {
// NOTE: the two DataSource instances are the same! // NOTE: the two DataSource instances ARE the same!
assertSame(dataSourceFromTxManager, dataSourceViaInjection); assertSame(dataSourceFromTxManager, dataSourceViaInjection);
} }

View File

@ -22,7 +22,6 @@ import static org.junit.Assert.assertNotSame;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.springframework.beans.Employee; import org.springframework.beans.Employee;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -45,7 +44,6 @@ import org.springframework.transaction.PlatformTransactionManager;
* @see Bean * @see Bean
* @see TransactionalAnnotatedConfigClassWithAtConfigurationTests * @see TransactionalAnnotatedConfigClassWithAtConfigurationTests
*/ */
@Ignore("Disabled until working within the build")
@ContextConfiguration(classes = TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class) @ContextConfiguration(classes = TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class)
public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests extends public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests extends
AbstractTransactionalAnnotatedConfigClassTests { AbstractTransactionalAnnotatedConfigClassTests {
@ -95,6 +93,8 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
public DataSource dataSource() { public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()// return new EmbeddedDatabaseBuilder()//
.addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")// .addScript("classpath:/org/springframework/test/context/junit4/spr9051/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build(); .build();
} }

View File

@ -11,6 +11,13 @@
</layout> </layout>
</appender> </appender>
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="File" value="build/spring-test.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="org.springframework.beans"> <logger name="org.springframework.beans">
<level value="warn" /> <level value="warn" />
</logger> </logger>
@ -21,7 +28,11 @@
<logger name="org.springframework.test.context.ContextLoaderUtils"> <logger name="org.springframework.test.context.ContextLoaderUtils">
<level value="warn" /> <level value="warn" />
</logger> </logger>
<!-- <!--
<logger name="org.springframework.test.context.support.DelegatingSmartContextLoader">
<level value="info" />
</logger>
<logger name="org.springframework.test.context.support.AbstractGenericContextLoader"> <logger name="org.springframework.test.context.support.AbstractGenericContextLoader">
<level value="info" /> <level value="info" />
</logger> </logger>
@ -29,14 +40,16 @@
<level value="info" /> <level value="info" />
</logger> </logger>
--> -->
<logger name="org.springframework.test.context.support">
<level value="fatal" /> <logger name="org.springframework.test.context">
<level value="warn" />
</logger> </logger>
<!-- Root Logger --> <!-- Root Logger -->
<root> <root>
<priority value="warn" /> <priority value="warn" />
<appender-ref ref="console" /> <appender-ref ref="console" />
<appender-ref ref="file" />
</root> </root>
</log4j:configuration> </log4j:configuration>