2012-04-17 100 views
6

我在嵌入模式下使用openEjb來測試無狀態會話bean,它具有一個注入EntityManager。但是,當我運行測試時,它會失敗,因爲它無法初始化應用程序。當我看到控制檯時,我也可以看到與無法找到persistence.xml有關的錯誤。openejb嵌入容器找不到persistence.xml

注意:我嘗試將WebContent以及META-INF放入類路徑中,但這也沒有幫助。

更新:

也有一些是關於使用OpenEJB的日食+資源時查找非常奇怪。

  1. 我將WebContent添加到源文件夾。我停止收到persistence.xml錯誤,但現在我得到一個錯誤,這表明openejb找不到任何託管資源
  2. 我將WebContent添加到源文件夾,但使用不同的輸出文件夾(對輸出文件夾使用允許輸出文件夾) ,並persistence.xml錯誤開始再次發生
  3. 我將META-INF複製到'測試',這是所有測試的源文件夾,並且一切運行正常
  4. 關於點1和2,問題似乎事實上,WEB-INF也被包含在WebContent中。如果我排除WEB-INF,那麼openejb可以在測試中找到persistence.xml,並且測試運行正常

我對於發生的事情一無所知。

我粘貼下面的所有相關錯誤和代碼。從失敗的JUnit測試在Eclipse

錯誤堆棧

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 

無狀態會話Bean:

@Stateless 
public class SlideService { 
    @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION) 
    private EntityManager em; 

    //various business methods not shown for brevity 
} 

的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> 

JUnit測試案例:

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 
} 

回答

7

我收到了同樣的異常,問題是兩個persistence.xml是在OpenEJB的的classpath中。

當我刪除其中一個時,問題就解決了。

異常消息是不是OpenEJB的一部分有用的在這裏,但如果你調試到org.apache.openejb.config.AppmodulehasFailures()hasErrors()方法,你可以在EjbModule.getValidation()通話時看到更多有用的信息! (版本4.0.0)。

1

這裏的主要問題是,在運行junit時,JUnit開始將WAR文件視爲JAR文件,因此試圖在META-INF的根目錄中查找persistance.xml,而在WAR文件中,META-INF將在內部創建WebContent文件夾。我只是在指向原始META-INF的src文件夾中創建了一個符號鏈接。和voilla ...............它的工作。 :)

2

Maven的創建,如下圖所示對於Web項目的目錄結構:

src->main-+->java 
      +->resources->META-INF 
      +->webapp->WEB-INF 

要解決您的問題,您應該創建目錄[META-INF]目錄下[資源]和地點的persistence.xml那裏。

相關問題