在 maven 中测试 运行 不清理 DBUNIT 数据库

Test running in maven not cleaning DBUNIT database

我正在使用以下依赖项来进行 dbunit 测试

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

测试 运行 在 inteliJ 中一次又一次地完美,但是当我 运行 在命令行上进行测试时,它总是失败,在我的测试中加载第二个测试时出现外键约束错误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)

我试过从 HSQL 切换到 H2,反之亦然,这两个数据库都出现了同样的问题。然而,运行 InteliJ 中的测试再次完美运行。在 Maven 中 运行ning 不断抛出错误。我在这里错过了什么我完全迷失了为什么它不起作用

我找到了...退后一步,重新思考,我发现默认情况下 运行s paralell 中的解决方案 maven 测试方法。如果您将 Maven 限制为仅 运行 类 in paralell 我能够修复它

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

这是一个非常烦人的"feature"追踪!