So, we need to write integration tests for our Java-configurated Spring Application (3.2.3) with an Oracle data base. There's a separate schema which gets populated by Spring-test-DbUnit and the first test runs without any problems. All the other following tests, no matter if they're in the same class or not, fail due to a closed connection:
java.sql.SQLRecoverableException: closed connection
at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3423)
at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3398)
at org.dbunit.database.statement.AbstractBatchStatement.<init>(AbstractBatchStatement.java:50)
at org.dbunit.database.statement.SimpleStatement.<init>(SimpleStatement.java:49)
at org.dbunit.database.statement.PreparedStatementFactory.createBatchStatement(PreparedStatementFactory.java:57)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:85)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:194)
at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:66)
at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:186)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:348)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Here's our data source:
@Override
@Bean
public DataSource dataSource()
{
String conString = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "john";
String password = "doe";
Driver driver = new OracleDriver();
SimpleDriverDataSource dataSource = new SimpleDriverDataSource(driver, conString, username, password);
return dataSource;
}
And this is a sample integration test class:
@ContextConfiguration(classes = { IntegrationTestConfiguration.class })
@ActiveProfiles(Constants.PROFILE_INTEGRATION)
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class, ForeignKeyDisabler.class })
@DbUnitConfiguration(databaseConnection = "oracleConnection")
@DatabaseSetup("/database/snapshot/snapshot1.xml")
public class IntegrationTest extends AbstractTransactionalJUnit4SpringContextTests
{
@Autowired
private NewsService newsService;
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testSpringConfiguration()
{
this.assertNewsSize(1);
News news1 = new News();
news1.setTitle("Test News 1");
News savedNews1 = this.newsService.save(news1);
Assert.assertTrue(savedNews1.getId() > 0);
News news2 = new News();
news2.setTitle("Test News 2");
News savedNews2 = this.newsService.save(news2);
Assert.assertTrue(savedNews2.getId() > 0);
News news3 = new News();
news3.setTitle("Test News 3");
News savedNews3 = this.newsService.save(news3);
Assert.assertTrue(savedNews3.getId() > 0);
this.assertNewsSize(4);
}
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testSpringConfigurationAgain()
{
this.assertNewsSize(1);
News news1 = new News();
news1.setTitle("Test News 1");
News savedNews1 = this.newsService.save(news1);
Assert.assertTrue(savedNews1.getId() > 0);
News news2 = new News();
news2.setTitle("Test News 2");
News savedNews2 = this.newsService.save(news2);
Assert.assertTrue(savedNews2.getId() > 0);
News news3 = new News();
news3.setTitle("Test News 3");
News savedNews3 = this.newsService.save(news3);
Assert.assertTrue(savedNews3.getId() > 0);
this.assertNewsSize(4);
}
private void assertNewsSize(int newsSize)
{
List<News> allNews = this.newsService.getNews();
Assert.assertEquals(newsSize, allNews.size());
}
}
Might this be problem of the Spring's database connection behavior to close a connection after committing/rolling back a transaction? If yes, how can I solve this? My last try was to create transaction on class/method base but without success.
Thank you in advance.
Thanks to freakman, we've got a solution that seems to work for us:
We created our own classes
OurOwnDbUnitTestExecutionListener.java
OurOwnDbUnitRunner.java
based on Spring-test-DbUnit's
com.github.springtestdbunit.DbUnitTestExecutionListener.java
com.github.springtestdbunit.DbUnitRunner.java
classes. Note that the package name for our classes must be equal due to the use of package-scoped classes. Since the listener uses the runner in a non-configurable manner, we use our own DbUnit runner:
private static OurOwnDbUnitRunner runner = new OurOwnDbUnitRunner();
The runner is modified so it does not close the connections after completing a test method. Line 116 is removed:
// testContext.getConnections().closeAll();
Finally, we use our modified DbUnitTestExecutionListener in our test class:
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
OurOwnDbUnitTestExecutionListener.class, // <<< this does the trick
ForeignKeyDisabler.class })