我正在使用以下依赖项来进行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中运行,不断抛出错误。我在这里错过了什么我完全失去了为什么它不起作用
答案 0 :(得分:2)
我发现了......退后一步,重新思考的事情发现我默认情况下在溶剂中运行测试方法。如果你将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>
这是一个非常烦人的“功能”,可以追踪!