gpt4 book ai didi

使用 Arquillian 进行 Java 集成测试、数据库清理

转载 作者:行者123 更新时间:2023-11-30 08:14:55 25 4
gpt4 key购买 nike

我正在进行一些集成测试,我需要在测试之间清理数据库,以便我可以做出正确的断言,并且测试不会导致诸如播种数据库时失败无法清理数据库。

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
}

严格的清理策略应该清除数据库,但由于外键而失败。因此,我尝试通过向 arquillian.xml 添加属性来禁用引用完整性

<property name="initStatement">SET REFERENTIAL_INTEGRITY FALSE</property>

怎样才能实现我想要的目标?有什么想法吗?

最佳答案

您可以在 persistence.xml 中将数据库模式设置为 drop-create。这将强制每次重新部署时从头开始重建数据库(这很重要!)

关于使用 Arquillian 进行 Java 集成测试、数据库清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802100/

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