Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit和Spring:找不到persistence.xml,但它确实存在_Java_Spring_Jpa_Junit_Persistence.xml - Fatal编程技术网

Java JUnit和Spring:找不到persistence.xml,但它确实存在

Java JUnit和Spring:找不到persistence.xml,但它确实存在,java,spring,jpa,junit,persistence.xml,Java,Spring,Jpa,Junit,Persistence.xml,我试图让JUnit运行,但它一直抛出以下错误: class path resource [META-INF/persistence.xml] cannot be opened because it does not exist 可以在此处找到整个stacktrace:。这是一个Spring项目,使用Hibernate(也使用JPA)。项目运行良好-在正常运行时(不运行JUnit测试),它不会抱怨任何丢失的文件 我尝试运行以下测试: @RunWith(SpringJUnit4ClassRunne

我试图让JUnit运行,但它一直抛出以下错误:

class path resource [META-INF/persistence.xml] cannot be opened because it does not exist
可以在此处找到整个stacktrace:。这是一个Spring项目,使用Hibernate(也使用JPA)。项目运行良好-在正常运行时(不运行JUnit测试),它不会抱怨任何丢失的文件

我尝试运行以下测试:

@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
    }
}
applicationContext.xml中的entityManagerFactory定义如下: (此处为整个文件:)

是的。它找不到persistence.xml,但它存在于它应该存在的地方(相对于类路径),项目只在运行JUnit时抱怨文件。有人知道发生了什么,或者我应该做些什么来让JUnit正常运行吗?我还不熟悉将JUnit与Spring/Hibernate结合使用,所以我可能做了一些错误的事情


我需要将META-INF/persistence.xml和applicationContext.xml移到src/main/resources,以便让JUnit找出所需文件的位置。JUnit测试用例中的以下行随后更改为:

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
(我还删除了对spring-security.xml的引用,因为它有点不必要)

非常感谢用户Wintermute

<?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>
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})