gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:12:40 26 4
gpt4 key购买 nike

我正在使用以下依赖项来进行 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中运行,不断抛出错误。我在这里错过了什么我完全迷失了为什么它不起作用

最佳答案

我发现了......退后一步并重新思考,我发现解决方案专家默认并行运行测试方法。如果您限制 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>

这是一个非常烦人的“功能”,需要追踪!

关于java - 在 maven 中运行测试不清理 DBUNIT 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com