因此,我们需要使用 Oracle 数据库为 Java 配置的 Spring Application (3.2.3)编写集成测试。Spring-test-DbUnit填充了一个单独的模式,第一个测试运行没有任何问题。以下所有其他测试,无论它们是否属于同一类,都由于连接关闭而失败:
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)
这是我们的数据源:
@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;
}
这是一个示例集成测试类:
@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());
}
}
这可能是 Spring 的数据库连接行为在提交/回滚事务后关闭连接的问题吗?如果是,我该如何解决这个问题?我最后一次尝试是在类/方法基础上创建事务,但没有成功。
先感谢您。