javamavenpersistencejboss-arquillianshrinkwrap

Cannot find test-persistence.xml using ShrinkWrap


I'm building an integration test with ShrinkWrap and I wish to test the persistence using a test file test-persistence.xml. According to the Arquillian Guides Testing Java Persistence, I need to write the test like this:

@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");
}

And here's my own implementation :

@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;
}

You can see that I've commented the line .addAsResource("META-INF/persistence.xml"), which aims to search the src/main/resources/META-INF/persistence.xml and add to the archive file. And it works fine. However, when I replaced it by the test-persistence.xml, then the maven build failed with error:

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)

Notice that :

Any idea about how to resolve this error ?


Annex

pom.xml

<testResources>
  <testResource>
    <filtering>true</filtering>
    <directory>src/test/resources</directory>
    <includes>
      <include>**/arquillian.xml</include>
      <include>**/arquillian.launch</include>
    </includes>
  </testResource>
</testResources>

Solution

  • Maven document Including and excluding files and directories describes the usage very clearly :

    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.

    To include a resource, we only need to add an <includes> element.

    <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>