Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 openejb嵌入式容器找不到persistence.xml_Java_Unit Testing_Jakarta Ee_Ejb_Openejb - Fatal编程技术网

Java openejb嵌入式容器找不到persistence.xml

Java openejb嵌入式容器找不到persistence.xml,java,unit-testing,jakarta-ee,ejb,openejb,Java,Unit Testing,Jakarta Ee,Ejb,Openejb,我在嵌入式模式下使用openEjb来测试一个无状态会话bean,它有一个注入的EntityManager。但是,当我运行测试时,它失败了,因为它无法初始化应用程序。当我看到控制台时,我还可以看到与找不到persistence.xml相关的错误 注意:我试着将WebContent和META-INF放在类路径中,但这也没有帮助 更新: Apache OpenEJB 4.0.0-beta-2 build: 20120115-08:26 http://openejb.apache.org/ INF

我在嵌入式模式下使用openEjb来测试一个无状态会话bean,它有一个注入的EntityManager。但是,当我运行测试时,它失败了,因为它无法初始化应用程序。当我看到控制台时,我还可以看到与找不到persistence.xml相关的错误

注意:我试着将WebContent和META-INF放在类路径中,但这也没有帮助

更新:

Apache OpenEJB 4.0.0-beta-2    build: 20120115-08:26
http://openejb.apache.org/
INFO - openejb.home = /home/user/workspace/wwald/slides
INFO - openejb.base = /home/user/workspace/wwald/slides
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Inspecting classpath for applications: 38 urls. Consider adjusting your exclude/include.  Current settings: openejb.deployments.classpath.exclude='', openejb.deployments.classpath.include='.*'
INFO - Found EjbModule in classpath: /home/user/workspace/wwald/slides/build/classes
INFO - Searched 38 classpath urls in 2000 milliseconds.  Average 52 milliseconds per url.
INFO - Beginning load: /home/user/workspace/wwald/slides/build/classes
INFO - Configuring enterprise application: /home/user/workspace/wwald/slides
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean SlideService: Container(type=STATELESS, id=Default Stateless Container)
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean com.diycomputerscience.slides.service.SlideServiceTest: Container(type=MANAGED, id=Default Managed Container)
INFO - Dumping Generated ejb-jar.xml to: /tmp/ejb-jar-5804778531295096416slides.xml
INFO - Dumping Generated openejb-jar.xml to: /tmp/openejb-jar-2921830618491817127slides.xml
ERROR - FAIL ... SlideService:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "entities"
ERROR - Invalid EjbModule(name=slides, path=/home/user/workspace/wwald/slides/build/classes)
INFO - Set the 'openejb.validation.output.level' system property to VERBOSE for increased validation details.
WARN - configureApplication.loadFailed
@Stateless
public class SlideService {
    @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION)
    private EntityManager em;

    //various business methods not shown for brevity
}
public class SlideServiceTest extends TestCase {

    private SlideService slideService;

    public SlideServiceTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();

        final Properties p = new Properties();
        p.put("myds", "new://Resource?type=DataSource");
        p.put("myds.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("myds.JdbcUrl", "jdbc:hsqldb:mem:slidedb");

        EJBContainer ejbContainer = EJBContainer.createEJBContainer();
        Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService");
        assertNotNull(oSlideService);
        this.slideService = (SlideService)oSlideService;
    }

    //not showing test methods for brevity, since the code fails in setUp itself
}
在使用openejb+eclipse时,资源查找有一些非常奇怪的地方

  • 我将WebContent添加到源文件夹中。我停止获取persistence.xml错误,但现在我得到一个错误,表明openejb找不到任何托管资源
  • 我将WebContent添加到源文件夹中,但使用了不同的输出文件夹(对源文件夹使用“允许输出文件夹”),persistence.xml错误再次开始出现
  • 我将META-INF复制到“test”中,这是所有测试的源文件夹,一切正常
  • 关于第1点和第2点,问题似乎是WEB-INF也作为WebContent的一部分被包括在内。如果排除WEB-INF,那么openejb同样可以在测试中找到persistence.xml,测试运行正常
  • 老实说,我对正在发生的事情一无所知

    我正在粘贴下面所有相关的错误和代码

    Eclipse中JUnit测试失败导致的错误堆栈

    org.apache.openejb.OpenEjbContainer$InvalidApplicationException: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=)
        at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:273)
        at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
        at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43)
        at com.diycomputerscience.slides.service.SlideServiceTest.setUp(SlideServiceTest.java:45)
        at junit.framework.TestCase.runBare(TestCase.java:128)
        at junit.framework.TestResult$1.protect(TestResult.java:106)
        at junit.framework.TestResult.runProtected(TestResult.java:124)
        at junit.framework.TestResult.run(TestResult.java:109)
        at junit.framework.TestCase.run(TestCase.java:120)
        at junit.framework.TestSuite.runTest(TestSuite.java:230)
        at junit.framework.TestSuite.run(TestSuite.java:225)
        at junit.framework.TestSuite.runTest(TestSuite.java:230)
        at junit.framework.TestSuite.run(TestSuite.java:225)
        at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=)
        at org.apache.openejb.config.ReportValidationResults.deploy(ReportValidationResults.java:82)
        at org.apache.openejb.config.AppInfoBuilder.build(AppInfoBuilder.java:264)
        at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:696)
        at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:267)
        ... 18 more
    
    我的控制台输出:

    Apache OpenEJB 4.0.0-beta-2    build: 20120115-08:26
    http://openejb.apache.org/
    INFO - openejb.home = /home/user/workspace/wwald/slides
    INFO - openejb.base = /home/user/workspace/wwald/slides
    INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
    INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
    INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
    INFO - Inspecting classpath for applications: 38 urls. Consider adjusting your exclude/include.  Current settings: openejb.deployments.classpath.exclude='', openejb.deployments.classpath.include='.*'
    INFO - Found EjbModule in classpath: /home/user/workspace/wwald/slides/build/classes
    INFO - Searched 38 classpath urls in 2000 milliseconds.  Average 52 milliseconds per url.
    INFO - Beginning load: /home/user/workspace/wwald/slides/build/classes
    INFO - Configuring enterprise application: /home/user/workspace/wwald/slides
    INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
    INFO - Auto-creating a container for bean SlideService: Container(type=STATELESS, id=Default Stateless Container)
    INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
    INFO - Auto-creating a container for bean com.diycomputerscience.slides.service.SlideServiceTest: Container(type=MANAGED, id=Default Managed Container)
    INFO - Dumping Generated ejb-jar.xml to: /tmp/ejb-jar-5804778531295096416slides.xml
    INFO - Dumping Generated openejb-jar.xml to: /tmp/openejb-jar-2921830618491817127slides.xml
    ERROR - FAIL ... SlideService:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "entities"
    ERROR - Invalid EjbModule(name=slides, path=/home/user/workspace/wwald/slides/build/classes)
    INFO - Set the 'openejb.validation.output.level' system property to VERBOSE for increased validation details.
    WARN - configureApplication.loadFailed
    
    @Stateless
    public class SlideService {
        @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION)
        private EntityManager em;
    
        //various business methods not shown for brevity
    }
    
    public class SlideServiceTest extends TestCase {
    
        private SlideService slideService;
    
        public SlideServiceTest(String name) {
            super(name);
        }
    
        protected void setUp() throws Exception {
            super.setUp();
    
            final Properties p = new Properties();
            p.put("myds", "new://Resource?type=DataSource");
            p.put("myds.JdbcDriver", "org.hsqldb.jdbcDriver");
            p.put("myds.JdbcUrl", "jdbc:hsqldb:mem:slidedb");
    
            EJBContainer ejbContainer = EJBContainer.createEJBContainer();
            Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService");
            assertNotNull(oSlideService);
            this.slideService = (SlideService)oSlideService;
        }
    
        //not showing test methods for brevity, since the code fails in setUp itself
    }
    
    无状态会话Bean:

    Apache OpenEJB 4.0.0-beta-2    build: 20120115-08:26
    http://openejb.apache.org/
    INFO - openejb.home = /home/user/workspace/wwald/slides
    INFO - openejb.base = /home/user/workspace/wwald/slides
    INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
    INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
    INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
    INFO - Inspecting classpath for applications: 38 urls. Consider adjusting your exclude/include.  Current settings: openejb.deployments.classpath.exclude='', openejb.deployments.classpath.include='.*'
    INFO - Found EjbModule in classpath: /home/user/workspace/wwald/slides/build/classes
    INFO - Searched 38 classpath urls in 2000 milliseconds.  Average 52 milliseconds per url.
    INFO - Beginning load: /home/user/workspace/wwald/slides/build/classes
    INFO - Configuring enterprise application: /home/user/workspace/wwald/slides
    INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
    INFO - Auto-creating a container for bean SlideService: Container(type=STATELESS, id=Default Stateless Container)
    INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
    INFO - Auto-creating a container for bean com.diycomputerscience.slides.service.SlideServiceTest: Container(type=MANAGED, id=Default Managed Container)
    INFO - Dumping Generated ejb-jar.xml to: /tmp/ejb-jar-5804778531295096416slides.xml
    INFO - Dumping Generated openejb-jar.xml to: /tmp/openejb-jar-2921830618491817127slides.xml
    ERROR - FAIL ... SlideService:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "entities"
    ERROR - Invalid EjbModule(name=slides, path=/home/user/workspace/wwald/slides/build/classes)
    INFO - Set the 'openejb.validation.output.level' system property to VERBOSE for increased validation details.
    WARN - configureApplication.loadFailed
    
    @Stateless
    public class SlideService {
        @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION)
        private EntityManager em;
    
        //various business methods not shown for brevity
    }
    
    public class SlideServiceTest extends TestCase {
    
        private SlideService slideService;
    
        public SlideServiceTest(String name) {
            super(name);
        }
    
        protected void setUp() throws Exception {
            super.setUp();
    
            final Properties p = new Properties();
            p.put("myds", "new://Resource?type=DataSource");
            p.put("myds.JdbcDriver", "org.hsqldb.jdbcDriver");
            p.put("myds.JdbcUrl", "jdbc:hsqldb:mem:slidedb");
    
            EJBContainer ejbContainer = EJBContainer.createEJBContainer();
            Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService");
            assertNotNull(oSlideService);
            this.slideService = (SlideService)oSlideService;
        }
    
        //not showing test methods for brevity, since the code fails in setUp itself
    }
    
    persistence.xml

    <persistence version="1.0"
        xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="entities">
            <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
            <jta-data-source>myds</jta-data-source>
            <non-jta-data-source>myds</non-jta-data-source>
            <properties>
                <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
            </properties>
        </persistence-unit>
    </persistence>
    

    我收到了相同的异常,问题是OpenEJB的类路径上有两个
    persistence.xml

    当我移除其中一个时,问题就解决了


    异常消息在OpenEJB中没有帮助,但是如果您调试到
    org.apache.OpenEJB.config.Appmodule
    hasFailures()
    hasErrors()
    方法中,您可以在
    EjbModule.getValidation()调用中看到更多有用的信息!(版本4.0.0)。

    这里的主要问题是,在运行junit时,junit开始将WAR文件视为JAR文件,因此尝试在根目录下查找META-INF中的persistence.xml,而在WAR文件中,META-INF是在WebContent文件夹中创建的。我只是在src文件夹中创建了一个符号链接,指向原始的META-INF和VOILA…………它起作用了。:)

    Maven为web项目创建目录结构,如下所示:

    src->main-+->java
              +->resources->META-INF
              +->webapp->WEB-INF
    
    要解决您的问题,您应该在目录[resources]下创建dir[META-INF],并将persistence.xml放在那里