使用ShrinkWrap

时间:2016-06-25 09:37:48

标签: java maven persistence jboss-arquillian shrinkwrap

我正在使用ShrinkWrap构建集成测试,我希望使用测试文件test-persistence.xml来测试持久性。根据Arquillian指南Testing Java Persistence,我需要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>

1 个答案:

答案 0 :(得分:1)

Maven文档Including and excluding files and directories非常清楚地描述了用法:

  

指定资源目录时,可能不会使用该目录中的每个文件。 因此,我们可能只需要指定要包含的文件,或者指定我们要排除的文件。

要包含资源,我们只需要添加<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>