Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在maven中运行的测试未清理DBUNIT数据库_Java_Maven_H2_Hsqldb_Dbunit - Fatal编程技术网

Java 在maven中运行的测试未清理DBUNIT数据库

Java 在maven中运行的测试未清理DBUNIT数据库,java,maven,h2,hsqldb,dbunit,Java,Maven,H2,Hsqldb,Dbunit,我使用以下依赖项来进行dbunit测试 <dependency> <groupId>org.dbunit</groupId> <artifactId>dbunit</artifactId> <version>2.4.9</version> <scope>test</scope>

我使用以下依赖项来进行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>

org.dbunit
单元测试
2.4.9
测试
org.hsqldb
hsqldb
2.3.4
测试
测试在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)

org.eclipse.persistence.jpa.PersistenceProvider
be.delijn.voertuigbeheer.entity.Regio
be.delijn.voertuigbeheer.entity.Stelplaats
be.delijn.voertuigbeheer.entity.VoertuigType
be.delijn.voertuigbeheer.entity.Voertuig
be.delijn.voertuigbeheer.entity.VoertuigEigenschap
假的
公共抽象类AbstractJPATest{
受保护的静态EntityManager工厂EntityManager工厂;
受保护的静态实体管理器实体管理器;
受保护的静态IDatabase连接;
受保护的静态IDataSet数据集;
@课前
公共静态void initEntityManager()引发异常{
System.out.println(“上课前”);
entityManagerFactory=Persistence.createEntityManagerFactory(“voertuigbeheer”);
entityManager=entityManagerFactory.createEntityManager();
ServerSession ServerSession=entityManager.unwrap(ServerSession.class);
SchemaManager SchemaManager=新SchemaManager(服务器会话);
schemaManager.replaceDefaultTables(true,true);
ConnectionPool ConnectionPool=serverSession.getConnectionPool(“默认”);
Connection dbconn=connectionPool.acquireConnection().getConnection();
连接=新数据库连接(dbconn);
DatabaseConfig=connection.getConfig();
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_工厂,新的HsqldbDataTypeFactory());
config.setProperty(DatabaseConfig.FEATURE\u限定表\u名称,true);
FlatXmlDataSetBuilder FlatXmlDataSetBuilder=新的FlatXmlDataSetBuilder();
flatXmlDataSetBuilder.setColumnSensing(true);
dataset=flatXmlDataSetBuilder.build(
Thread.currentThread().getContextClassLoader().getResourceAsStream(“test dataset.xml”);
}
@下课
公共静态void closeEntityManager(){
entityManager.close();
entityManagerFactory.close();
}
@以前
public void cleanDB()引发异常{
系统输出打印项次(“加载”);
DatabaseOperation.CLEAN_INSERT.execute(连接,数据集);
设置();
}
公共摘要无效设置();
}
公共类RegioDaoTests扩展了AbstractJPATest{
私人区道区道;
@试验
public void getAll()引发异常{
List result=regioDao.getAll();
断言(result.size()).isEqualTo(2);
}
@试验
public void getById()引发异常{
Regio Regio=regioDao.getById(2L);
断言(regio.getId()).isEqualTo(2);
}
@凌驾
公共作废设置(){
regioDao=新regioDao();
regioDao.setEntityManager(entityManager);
}
}
原因:org.hsqldb.hsqldb异常:完整性约束冲突:唯一约束或索引冲突;系统PK_10234表:区域
位于org.hsqldb.error.error.error(未知来源)
位于org.hsqldb.Constraint.getException(未知源)
位于org.hsqldb.index.IndexAVLMemory.insert(未知源)
位于org.hsqldb.persist.RowStoreAVL.indexRow(未知源)
位于org.hsqldb.TransactionManager2PL.addInsertAction(未知源)
位于org.hsqldb.Session.addInsertAction(未知源)
位于org.hsqldb.Table.insertSingleRow(未知源)
位于org.hsqldb.StatementDML.insertSingleRow(未知源)
位于org.hsqldb.StatementInsert.getResult(未知源)
位于org.hsqldb.StatementDMQL.execute(未知源)
位于org.hsqldb.Session.executeCompiledStatement(未知源)
位于org.hsqldb.Session.execute(未知源)

我尝试过从HSQL切换到H2,反之亦然。两个数据库都会出现相同的问题。然而,在InteliJ中运行测试同样可以完美地工作。它在maven中运行,不断抛出错误。我在这里错过了什么我完全不知道为什么它不起作用了我找到了它。。。退一步重新思考后,我发现sollution maven默认并行运行测试方法。如果您限制maven只并行运行类,我就能够修复它

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

voertuigbeheer网站
org.apache.maven.plugins
maven surefire插件
2.19.1
班级

这是一个非常讨厌的“功能”追踪

我找到了。。。退一步重新思考后,我发现sollution maven默认并行运行测试方法。如果您限制maven只并行运行类,我就能够修复它

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

voertuigbeheer网站
org.apache.maven.plugins
maven surefire插件
2.19.1
班级
这是一个非常讨厌的“功能”追踪