the object is already closed in integration test

2k views Asked by At

I am using spring-test-dbunit to run integration tests. I want to use different datasets inside one class.

I have BaseRepositoryTest class

@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
        }
    }

And BaseAnnotatedRepositoryTest class

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

And here is the test

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() {
    }

}

When I attempt to run the test I get the error with that cause

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)

If I separate those test methods - for example move every method to its own class - it works fine.

1

There are 1 answers

0
lapots On BEST ANSWER

When I changed AFTER_CLASS to AFTER_EACH_TEST_METHOD it started to work fine. (though the time increased considerably)