Question

I'm trying to get JUnit to run, but it keeps throwing the following error:

class path resource [META-INF/persistence.xml] cannot be opened because it does not exist

The entire stacktrace can be found here: http://codepad.org/OhlyjQKn. 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)

I try to run the following test:

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

The entityManagerFactory in applicationContext.xml is defined as follows: (entire file here: http://codepad.org/UrCtj0pW)

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="hsqldb-ds" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>

my classpath is src/main/webapp/WEB-INF/classes - which holds applicationContext.xml and also META-INF/persistence.xml

The persistence.xml is defined as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<persistence-unit name="hsqldb-ds" transaction-type="RESOURCE_LOCAL">

    <description>HSQLDB Persistence Unit</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <properties>            
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:demodb" />
        <property name="javax.persistence.jdbc.user" value="sa" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.transaction.flush_before_completion" value="true" />
    </properties>
</persistence-unit>

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.

Kind regards

Was it helpful?

Solution

I needed to move the META-INF/persistence.xml and applicationContext.xml to src/main/resources to get JUnit to figure out where the necessary files where. The following line in the JUnit test case then changed to this:

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

(I also removed the reference to spring-security.xml because it was kind of unnecessary)

Big thanks to user Wintermute!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top