java - 该对象已在集成测试中关闭

标签 java dbunit spring-test-dbunit

我正在使用spring-test-dbunit来运行集成测试。我想在一个类中使用不同的数据集。

我有BaseRepositoryTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {
        "classpath:path/to/context/context.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@TransactionConfiguration()
public abstract class BaseRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
    /** Database connection. */
    @Autowired
    protected IDatabaseConnection databaseConnection;


    /**
     * Configures database connection.
     */
    @PostConstruct
    public void initConfig() {
        try {
            databaseConnection.getConnection().setAutoCommit(true);
        } catch (SQLException e) {
            // throws exception
        }
    }

    /**
     * Load dataset data.
     *
     * @param datasetPath path to dataset
     */
    protected void loadData(String datasetPath) {
        try {
            IDataSet dataset = new FlatXmlDataSetBuilder()
                    .setColumnSensing(true)
                    .build(new ClassPathResource(datasetPath).getFile());
            DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataset);
        } catch (Exception e) {
            // throws exception
        }
    }

BaseAnnotatedRepositoryTest

@DbUnitConfiguration(databaseConnection = { "databaseConnection" })
@TestExecutionListeners(DbUnitTestExecutionListener.class)
public abstract class BaseAnnotatedRepositoryTest extends BaseRepositoryTest {

这是测试

public class PrototypeTest extends BaseAnnotatedRepositoryTest {

    private static final String TEST_DATASET =
            "testDataset.xml";

    @Autowired
    private IQuestionRepository repository;

    @Test
    @DatabaseSetup(TEST_DATASET)
    public void testSimple() {
    }

    @Test
    @DatabaseSetup(TEST_DATASET)
    public void anotherTestSimple() {
    }

}

当我尝试运行测试时,我收到了与该原因相关的错误

org.h2.jdbc.JdbcSQLException: The object is already closed [90007-170]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.message.DbException.get(DbException.java:135)
    at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1388)
    at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1366)
    at org.h2.jdbc.JdbcConnection.setAutoCommit(JdbcConnection.java:406)

如果我将这些测试方法分开 - 例如将每个方法移至其自己的类 - 它工作正常。

最佳答案

当我将 AFTER_CLASS 更改为 AFTER_EACH_TEST_METHOD 时,它开始正常工作。 (虽然时间大大增加)

关于java - 该对象已在集成测试中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41301557/

相关文章:

java: 将文件解压成字符串太慢

java - 我在哪里排序?

java - iText 图像调整大小

java - dbunit 模式参数不起作用

junit - 错误 NoSuchColumnException DbUnit

postgresql - 带 Postgresql 的 DBUnit 和列名区分大小写 - 它是如何工作的?

java - 如何验证 oAuth2 access_token 是否被在 Spring security 中颁发给它的同一个客户端使用?

dbunit - 在 dbunit 中使用 CsvDataFileLoader 加载空值

postgresql - DBUnit bool 值

java - 使用DBUnit时如何防止数据删除