繁体   English   中英

测试在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中完美运行,但是当我在命令行上运行测试时,它总是失败,并且在将第二个测试加载到测试类中时出现外键约束错误

<?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中运行,不断抛出错误。 我在这里想念的是我完全迷失了为什么它不起作用

我发现了……退后一步,重新思考一下事情,发现我自己是解决方案专家,默认情况下在paralell中运行测试方法。 如果您将Maven限制为仅在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>

这真是个令人讨厌的“功能”!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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