无法使用 ShrinkWrap 找到 test-persistence.xml
Cannot find test-persistence.xml using ShrinkWrap
我正在使用 ShrinkWrap 构建集成测试,我希望使用测试文件 test-persistence.xml
来测试持久性。根据 Arquillian 指南 Testing Java Persistence, I need to write the test 是这样的:
@Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Game.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
这是我自己的实现:
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addPackages(true, "org.hibernate.search.jsr352")
.addPackages(true, "javax.persistence")
.addPackages(true, "org.hibernate.search.annotations")
.addClass(Serializable.class)
.addClass(Date.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
// .addAsResource("META-INF/persistence.xml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsResource("META-INF/batch-jobs/mass-index.xml");
return war;
}
你可以看到我已经注释了.addAsResource("META-INF/persistence.xml")
行,它旨在搜索src/main/resources/META-INF/persistence.xml
并添加到存档文件中。它工作正常。 然而,当我用 test-persistence.xml
替换它时,maven 构建失败并出现错误:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.715 sec <<< FAILURE! - in org.hibernate.search.jsr352.DeploymentIT
org.hibernate.search.jsr352.DeploymentIT Time elapsed: 6.714 sec <<< ERROR!
java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive org.hibernate.search.jsr352.DeploymentIT.createDeployment()
at org.jboss.shrinkwrap.impl.base.Validate.notNull(Validate.java:43)
at org.jboss.shrinkwrap.impl.base.container.ContainerBase.fileFromResource(ContainerBase.java:1990)
at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addAsResource(ContainerBase.java:1056)
at org.hibernate.search.jsr352.DeploymentIT.createDeployment(DeploymentIT.java:67)
注意:
- 测试持久性文件位于
src/test/resources/test-persistence.xml
- 测试资源
src/test/resources
文件夹应该已经包含在maven pom文件中(见附件)。
知道如何解决这个错误吗?
附件
pom.xml
<testResources>
<testResource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
<includes>
<include>**/arquillian.xml</include>
<include>**/arquillian.launch</include>
</includes>
</testResource>
</testResources>
Maven 文档Including and excluding files and directories 描述的很清楚:
When specifying a resource directory, every file within that directory may not be used. Thus, we may have to specify only the files that we want to include or specify the files that we want to exclude.
要包含资源,我们只需要添加一个 <includes>
元素。
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>[your directory]</directory>
<includes>
<include>[resource file #1]</include>
<include>[resource file #2]</include>
<include>[resource file #3]</include>
...
<include>[resource file #n]</include>
</includes>
</resource>
...
</resources>
...
</build>
...
</project>
我正在使用 ShrinkWrap 构建集成测试,我希望使用测试文件 test-persistence.xml
来测试持久性。根据 Arquillian 指南 Testing Java Persistence, I need to write the test 是这样的:
@Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Game.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
这是我自己的实现:
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addPackages(true, "org.hibernate.search.jsr352")
.addPackages(true, "javax.persistence")
.addPackages(true, "org.hibernate.search.annotations")
.addClass(Serializable.class)
.addClass(Date.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
// .addAsResource("META-INF/persistence.xml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsResource("META-INF/batch-jobs/mass-index.xml");
return war;
}
你可以看到我已经注释了.addAsResource("META-INF/persistence.xml")
行,它旨在搜索src/main/resources/META-INF/persistence.xml
并添加到存档文件中。它工作正常。 然而,当我用 test-persistence.xml
替换它时,maven 构建失败并出现错误:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.715 sec <<< FAILURE! - in org.hibernate.search.jsr352.DeploymentIT
org.hibernate.search.jsr352.DeploymentIT Time elapsed: 6.714 sec <<< ERROR!
java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive org.hibernate.search.jsr352.DeploymentIT.createDeployment()
at org.jboss.shrinkwrap.impl.base.Validate.notNull(Validate.java:43)
at org.jboss.shrinkwrap.impl.base.container.ContainerBase.fileFromResource(ContainerBase.java:1990)
at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addAsResource(ContainerBase.java:1056)
at org.hibernate.search.jsr352.DeploymentIT.createDeployment(DeploymentIT.java:67)
注意:
- 测试持久性文件位于
src/test/resources/test-persistence.xml
- 测试资源
src/test/resources
文件夹应该已经包含在maven pom文件中(见附件)。
知道如何解决这个错误吗?
附件
pom.xml
<testResources>
<testResource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
<includes>
<include>**/arquillian.xml</include>
<include>**/arquillian.launch</include>
</includes>
</testResource>
</testResources>
Maven 文档Including and excluding files and directories 描述的很清楚:
When specifying a resource directory, every file within that directory may not be used. Thus, we may have to specify only the files that we want to include or specify the files that we want to exclude.
要包含资源,我们只需要添加一个 <includes>
元素。
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>[your directory]</directory>
<includes>
<include>[resource file #1]</include>
<include>[resource file #2]</include>
<include>[resource file #3]</include>
...
<include>[resource file #n]</include>
</includes>
</resource>
...
</resources>
...
</build>
...
</project>