我正在进行一些集成测试,我需要在测试之间清理数据库,以便我可以做出正确的断言,并且测试不会导致Failed while seeding database
或Unable to clean database.
之类的错误。
pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.2.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.2.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-api</artifactId>
<version>1.0.0.Alpha5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-impl</artifactId>
<version>1.0.0.Alpha5</version>
<scope>test</scope>
</dependency>
一些测试类:
@RunWith(Arquillian.class)
@UsingDataSet("datasets/empty.yml")
public class SomeServiceCase {
@EJB
SomeService someService;
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addPackages(true, "vo")
.addPackages(true, "service")
.addPackages(true, "domain")
.addAsManifestResource("test-persistence.xml", ArchivePaths.create("persistence.xml"));
}
@Test
@UsingDataSet("datasets/someModel/someModels.yml")
@Cleanup(phase = TestExecutionPhase.AFTER, strategy = CleanupStrategy.STRICT)
public void teastSomething() {
//modifies database content here
}
@Test
@UsingDataSet("datasets/someModel/someModels.yml")
@Cleanup(phase = TestExecutionPhase.AFTER, strategy = CleanupStrategy.STRICT)
public void testSomethingElse() {
//needs initial database content for tests to work
}
严格的清理策略应该清除数据库但由于外键而失败。所以我尝试通过向Referential Integrity
arquillian.xml
<property name="initStatement">SET REFERENTIAL_INTEGRITY FALSE</property>
我怎样才能实现我想要的目标?有什么想法吗?
答案 0 :(得分:1)
您可以在persistence.xml中设置数据库模式以进行drop-create。这将迫使每次重新部署从头开始重建数据库(它的东西!)
答案 1 :(得分:0)
您必须仔细查看错误消息的堆栈跟踪。通常,根据我的经验,由于外键约束,数据库无法清理......但细节应该出现在堆栈跟踪中。
我不知道您对参照完整性的停用是否有效。我使用注释@ApplyScriptBefore来禁用它。
答案 2 :(得分:0)
我喜欢使用liquibase来解决这个问题。它很容易用作maven插件,如下所示:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<executions>
<execution>
<id>update_test</id>
<phase>process-test-classes</phase>
<configuration>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<url>${test.db.connectionURL}</url>
<defaultSchemaName>${test.hibernate.default_schema}</defaultSchemaName>
<username>${test.db.username}</username>
<password>${test.db.password}</password>
<changeLogFile>db/db.changelog.xml</changeLogFile>
<contexts>test</contexts>
<changelogSchemaName>${test.hibernate.default_schema}</changelogSchemaName>
</configuration>
<goals>
<goal>dropAll</goal>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
执行删除和更新的目标是:
<goal>dropAll</goal>
<goal>update</goal>