title: JUnit and Spring: Cannot find persistence.xml - but it does exist
body: <p>I'm trying to get JUnit to run, but it keeps throwing the following error: </p>
  
  <pre><code>class path resource [META-INF/persistence.xml] cannot be opened because it does not exist
  </code></pre>
  
  <p>The entire stacktrace can be found here: <a href="http://codepad.org/OhlyjQKn" rel="nofollow">http://codepad.org/OhlyjQKn</a>. It is a Spring project, with Hibernate (also using JPA). The project runs fine - it doesn't complain about any missing files when running it normally (not running the JUnit test)</p>
  
  <p>I try to run the following test: </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/classes/applicationContext.xml",
                                      "file:src/main/webapp/WEB-INF/config/spring-security.xml"})
  public class KeyServiceTests {
  
      @Test
      public void testKeyCreation() {
          ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
          KeyService keyService = (KeyService) context.getBean("keyService");
          // Some testing goes on here
      }
  }
  </code></pre>
  
  <p>The entityManagerFactory in applicationContext.xml is defined as follows:
  (entire file here: <a href="http://codepad.org/UrCtj0pW" rel="nofollow">http://codepad.org/UrCtj0pW</a>)</p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitName" value="hsqldb-ds" /&gt;
      &lt;property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>my classpath is <code>src/main/webapp/WEB-INF/classes</code> - which holds applicationContext.xml and also <code>META-INF/persistence.xml</code></p>
  
  <p>The persistence.xml is defined as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  &lt;persistence-unit name="hsqldb-ds" transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;description&gt;HSQLDB Persistence Unit&lt;/description&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
      &lt;properties&gt;            
          &lt;property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" /&gt;
          &lt;property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:demodb" /&gt;
          &lt;property name="javax.persistence.jdbc.user" value="sa" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="" /&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="validate" /&gt;
          &lt;property name="hibernate.show_sql" value="false" /&gt;
          &lt;property name="hibernate.format_sql" value="true" /&gt;
          &lt;property name="hibernate.transaction.flush_before_completion" value="true" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>So yea. It can't find persistence.xml, but it exists where it should exist (relative to the classpath), and the project only complains about the file when running JUnit. Does anyone know what's going on, or what I should be doing to get JUnit to run properly? I'm new to using JUnit with Spring/Hibernate, so I'm probably doing a few things wrong.</p>
  
  <p>Kind regards</p>