简体   繁体   中英

Test running in maven not cleaning DBUNIT database

I'm using following dependencies to get a dbunit test going

        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.4</version>
            <scope>test</scope>
        </dependency>

The test runs perfectly in inteliJ over and over but when I run the test on command line it fails all the time with foreign key constraint errors on loading the second test in my test class

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <PUBLIC.REGIO id="1" entiteit="1" code="a" naam="regio 1"/>
    <PUBLIC.REGIO id="2" entiteit="2" code="b" naam="regio 2"/>

    <PUBLIC.REGIO />
</dataset>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="voertuigbeheer" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>be.delijn.voertuigbeheer.entity.Regio</class>
    <class>be.delijn.voertuigbeheer.entity.Stelplaats</class>
    <class>be.delijn.voertuigbeheer.entity.VoertuigType</class>
    <class>be.delijn.voertuigbeheer.entity.Voertuig</class>
    <class>be.delijn.voertuigbeheer.entity.VoertuigEigenschap</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.logging.thread" value="false"/>
        <property name="eclipselink.logging.session" value="false"/>
        <property name="eclipselink.logging.timestamp" value="false"/>
        <property name="eclipselink.logging.exceptions" value="false"/>

        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsql:mem"/>
        <property name="javax.persistence.jdbc.user" value="sa"/>
        <property name="javax.persistence.jdbc.password" value=""/>
    </properties>
  </persistence-unit>
</persistence>

public abstract class AbstractJPATest {

    protected static EntityManagerFactory entityManagerFactory;
    protected static EntityManager entityManager;
    protected static IDatabaseConnection connection;
    protected static IDataSet dataset;

    @BeforeClass
    public static void initEntityManager() throws Exception {
        System.out.println("before class running");
        entityManagerFactory = Persistence.createEntityManagerFactory("voertuigbeheer");
        entityManager = entityManagerFactory.createEntityManager();

        ServerSession serverSession = entityManager.unwrap(ServerSession.class);
        SchemaManager schemaManager = new SchemaManager(serverSession);
        schemaManager.replaceDefaultTables(true, true);
        ConnectionPool connectionPool = serverSession.getConnectionPool("default");
        Connection dbconn = connectionPool.acquireConnection().getConnection();
        connection = new DatabaseConnection(dbconn);
        DatabaseConfig config = connection.getConfig();
        config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
        config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

        FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
        flatXmlDataSetBuilder.setColumnSensing(true);
        dataset = flatXmlDataSetBuilder.build(
                Thread.currentThread().getContextClassLoader().getResourceAsStream("test-dataset.xml"));
    }

    @AfterClass
    public static void closeEntityManager() {
        entityManager.close();
        entityManagerFactory.close();
    }

    @Before
    public void cleanDB() throws Exception {
        System.out.println("loading");
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
        setup();
    }

    public abstract void setup();
}

public class RegioDaoTests extends AbstractJPATest {

    private RegioDao regioDao;

    @Test
    public void getAll() throws Exception {
        List<Regio> result = regioDao.getAll();
        assertThat(result.size()).isEqualTo(2);
    }

    @Test
    public void getById() throws Exception {
        Regio regio = regioDao.getById(2L);
        assertThat(regio.getId()).isEqualTo(2);
    }

    @Override
    public void setup() {
        regioDao = new RegioDao();
        regioDao.setEntityManager(entityManager);
    }
}

Caused by: org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; SYS_PK_10234 table: REGIO
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.Constraint.getException(Unknown Source)
        at org.hsqldb.index.IndexAVLMemory.insert(Unknown Source)
        at org.hsqldb.persist.RowStoreAVL.indexRow(Unknown Source)
        at org.hsqldb.TransactionManager2PL.addInsertAction(Unknown Source)
        at org.hsqldb.Session.addInsertAction(Unknown Source)
        at org.hsqldb.Table.insertSingleRow(Unknown Source)
        at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
        at org.hsqldb.StatementInsert.getResult(Unknown Source)
        at org.hsqldb.StatementDMQL.execute(Unknown Source)
        at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
        at org.hsqldb.Session.execute(Unknown Source)

I've tried switching from HSQL to H2 and vice versa both databases give the same problem. However again running the tests in InteliJ work perfectly. It's running in maven that keeps throwing the error. What am I missing here I'm totally lost why it wouldn't work

I found it ... taking a step back and rethinking things found me the sollution maven by default runs test methods in paralell. If you restrict maven to only run classes in paralell I was able to fix it

<build>
    <finalName>voertuigbeheer-web</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <parallel>classes</parallel>
            </configuration>
        </plugin>
    </plugins>
</build>

This was a very annoying "feature" to track down !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM