title: JUnit test with JPA & Hibernate : Persistence.xml
body: <p>I'm working with 3 layers : Services, Business and Persistence. I want to test each layer. So, I create a src/test/java folder with my test file and a src/test/resources for persistence files (persistence.xml and data.sql). I set this two folders in each layer. But when I want to test the business layer, I got the following errors :</p>
  
  <pre><code>SEVERE - Unable to load Persistence Unit from EAR: C:\dev\prj\myProject\persistence\\classpath.ear, module: file:/c:/dev/myProject/persistence/target/classes/. Exception: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are &lt;{http://java.sun.com/xml/ns/persistence}persistence&gt;
  javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are &lt;{http://java.sun.com/xml/ns/persistence}persistence&gt;
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
      at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
      at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
      at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
      at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
      at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:619)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3129)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:504)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
      at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
      at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
      at org.xml.sax.helpers.XMLFilterImpl.parse(XMLFilterImpl.java:357)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
      at org.apache.openejb.jee.jpa.unit.JaxbPersistenceFactory.getPersistence(JaxbPersistenceFactory.java:64)
      at org.apache.openejb.config.ReadDescriptors.deploy(ReadDescriptors.java:180)
      at org.apache.openejb.config.ConfigurationFactory$Chain.deploy(ConfigurationFactory.java:403)
      at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:971)
      at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:832)
      at org.apache.openejb.config.ConfigurationFactory.getOpenEjbConfiguration(ConfigurationFactory.java:546)
      at org.apache.openejb.config.ConfigurationFactory.getOpenEjbConfiguration(ConfigurationFactory.java:591)
      at org.apache.openejb.assembler.classic.Assembler.getOpenEjbConfiguration(Assembler.java:484)
      at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:463)
      at org.apache.openejb.OpenEJB$Instance.&lt;init&gt;(OpenEJB.java:151)
      at org.apache.openejb.OpenEJB$Instance.&lt;init&gt;(OpenEJB.java:68)
      at org.apache.openejb.OpenEJB.init(OpenEJB.java:309)
      at org.apache.openejb.OpenEJB.init(OpenEJB.java:289)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.apache.openejb.loader.OpenEJBInstance.init(OpenEJBInstance.java:36)
      at org.apache.openejb.core.LocalInitialContextFactory.init(LocalInitialContextFactory.java:98)
      at org.apache.openejb.core.LocalInitialContextFactory.init(LocalInitialContextFactory.java:62)
      at org.apache.openejb.core.LocalInitialContextFactory.getInitialContext(LocalInitialContextFactory.java:46)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
      at javax.naming.InitialContext.init(InitialContext.java:244)
      at javax.naming.InitialContext.&lt;init&gt;(InitialContext.java:216)
      at be.smals.dos.engine.persistence.test.WorkflowDaoTest.setUp(WorkflowDaoTest.java:55)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
      at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
      at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
  </code></pre>
  
  <p>After a few researches, it seems that we cannot have more than one persistence.xml.
  To resolve the problem, I tried following solutions :</p>
  
  <ol>
  <li>Replace the xml file of the business layer to the persistence layer. So the test run the persistence.xml of the business layer.</li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;plugin&gt;
   &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
   &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
   &lt;version&gt;1.8&lt;/version&gt;
   &lt;executions&gt;
     &lt;execution&gt;
    &lt;id&gt;copy-test-persistence&lt;/id&gt;
    &lt;phase&gt;process-test-resources&lt;/phase&gt;
    &lt;goals&gt;
      &lt;goal&gt;run&lt;/goal&gt;
    &lt;/goals&gt;
    &lt;configuration&gt;
      &lt;tasks&gt;
     &lt;!-- backup the "src" persistence.xml --&gt;
     &lt;move file="${project.build.outputDirectory}/META-INF/persistence.xml"
        tofile="${project.build.outputDirectory}/META-INF/persistence.xml.src"/&gt;
     &lt;!-- copy the "test" persistence.xml --&gt;
     &lt;copy file="${project.build.testOutputDirectory}/META-INF/persistence.xml"
        tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/&gt;
     &lt;!-- backup the "src" dos-engine-data.sql --&gt;
     &lt;move file="${project.build.outputDirectory}/data.sql"
        tofile="${project.build.outputDirectory}/data.sql.src"/&gt;
     &lt;!-- copy the "test" dos-engine-data.sql --&gt;
     &lt;copy file="${project.build.testOutputDirectory}/data.sql"
        tofile="${project.build.outputDirectory}/data.sql"/&gt;
      &lt;/tasks&gt;
    &lt;/configuration&gt;
     &lt;/execution&gt;
     &lt;execution&gt;
    &lt;id&gt;restore-persistence&lt;/id&gt;
    &lt;phase&gt;prepare-package&lt;/phase&gt;
    &lt;goals&gt;
      &lt;goal&gt;run&lt;/goal&gt;
    &lt;/goals&gt;
    &lt;configuration&gt;
      &lt;tasks&gt;
     &lt;!-- restore the "src" persistence.xml --&gt;
     &lt;move file="${project.build.outputDirectory}/META-INF/persistence.xml.src"
        tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/&gt;
     &lt;!-- restore the "src" dos-engine-data.sql --&gt;
     &lt;move file="${project.build.outputDirectory}/data.sql.src"
        tofile="${project.build.outputDirectory}/data.sql"/&gt;
      &lt;/tasks&gt;
    &lt;/configuration&gt;
     &lt;/execution&gt;
   &lt;/executions&gt;
  &lt;/plugin&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <ol start="2">
  <li>Creation of a second jar but without any persistence.xml. And indicate that when we run test we have to use those JAR.</li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;plugin&gt;
   &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
   &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
   &lt;executions&gt;
    &lt;execution&gt;
     &lt;phase&gt;package&lt;/phase&gt;
     &lt;goals&gt;
      &lt;goal&gt;jar&lt;/goal&gt;
     &lt;/goals&gt;
     &lt;configuration&gt;
      &lt;finalName&gt;jarName&lt;/finalName&gt;
      &lt;includes&gt;
       &lt;include&gt;pathWithPersistenceFile&lt;/include&gt;
      &lt;/includes&gt;
     &lt;/configuration&gt;
    &lt;/execution&gt;
    &lt;execution&gt;
     &lt;phase&gt;package&lt;/phase&gt;
     &lt;goals&gt;
      &lt;goal&gt;jar&lt;/goal&gt;
     &lt;/goals&gt;
     &lt;configuration&gt;
      &lt;finalName&gt;jarNameTest&lt;/finalName&gt;
      &lt;classifier&gt;test&lt;/classifier&gt;
      &lt;includes&gt;
       &lt;include&gt;pathWithoutPersistenceFile&lt;/include&gt;
      &lt;/includes&gt;
     &lt;/configuration&gt;
    &lt;/execution&gt;
   &lt;/executions&gt;
  &lt;/plugin&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Those two solution aren't working in my case. Is there an other issue to work with more than one persistence.xml ? Is there maybe an solution to specify an other persistence file path but without Spring ? In this project, I'm working with Maven, Hibernate and H2 mem.</p>
  
  <p>I spend several days to find a solution for my specific problem, but I don't find something helpful for my problem.</p>
  
  <p>Anyone any idea ?</p>
  
  <p>Thanks</p>