0

因此,我们需要使用 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 的数据库连接行为在提交/回滚事务后关闭连接的问题吗?如果是,我该如何解决这个问题?我最后一次尝试是在类/方法基础上创建事务,但没有成功。

先感谢您。

4

3 回答 3

2

我相信它与您的连接DirtiesContextTestExecutionListener.class是某种等效的 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html

指示与测试关联的 ApplicationContext 是脏的并且应该关闭的测试注释:...

在当前测试类中的每个测试方法之后,当在类级别声明且类模式设置为 AFTER_EACH_TEST_METHOD

所以可能春天在第一种测试方法之后关闭你的豆子。也许在 @AfterMethod 方法中清理数据库更容易?它会在每次测试后运行。

于 2015-07-30T11:14:08.767 回答
0

如果您使用的是 spring-test-dbunit,另一种解决方案可能是提供一个覆盖关闭方法的 dbUnitDatabaseconnection:

@Bean
public IDatabaseConnection dbUnitDatabaseConnection(DataSource dataSource) throws SQLException, DatabaseUnitException {
    return new CustomDatabaseConnection(dataSource.getConnection());
}

private static class CustomDatabaseConnection extends DatabaseConnection {
    public CustomDatabaseConnection(Connection connection) throws DatabaseUnitException {
        super(connection);
    }


    @Override
    public void close() {
        // don't close the connection
    }
}
于 2020-03-23T15:31:56.963 回答
0

多亏了freakman,我们有了一个似乎对我们有用的解决方案:

我们创建了自己的课程

  • OurOwnDbUnitTestExecutionListener.java
  • OurOwnDbUnitRunner.java

基于 Spring-test-DbUnit 的

  • com.github.springtestdbunit.DbUnitTestExecutionListener.java
  • com.github.springtestdbunit.DbUnitRunner.java

类。请注意,由于使用包范围的类,我们的类的包名称必须相同。由于侦听器以不可配置的方式使用运行器,因此我们使用自己的 DbUnit 运行器:

private static OurOwnDbUnitRunner runner = new OurOwnDbUnitRunner();

运行器已修改,因此它不会在完成测试方法后关闭连接。删除第 116 行:

// testContext.getConnections().closeAll();

最后,我们在测试类中使用修改后的 DbUnitTestExecutionListener: @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, OurOwnDbUnitTestExecutionListener.class, // <<< this does the trick ForeignKeyDisabler.class })

于 2015-07-30T12:46:18.860 回答