6

組み込みモードで openEjb を使用して、注入された EntityManager を持つステートレス セッション Bean をテストしています。ただし、テストを実行すると、アプリケーションを初期化できなかったため失敗します。コンソールを見ると、persistence.xml が見つからないことに関連するエラーも表示されます。

注: WebContent と META-INF をクラスパスに入れてみましたが、どちらも役に立ちません。

アップデート:

openejb + eclipse を使用する場合、リソース ルックアップに非常に奇妙な点があります。

  1. WebContent をソース フォルダーに追加しました。persistence.xml エラーが発生しなくなりましたが、openejb が管理対象リソースを見つけられなかったことを示すエラーが発生するようになりました
  2. WebContent をソース フォルダーに追加しましたが、別の出力フォルダー (ソース フォルダーに許可出力フォルダーを使用) を使用すると、persistence.xml エラーが再び発生し始めます。
  3. META-INF をすべてのテストのソース フォルダーである「test」にコピーしたところ、すべて正常に実行されました。
  4. ポイント1と2に関しては、問題は、WEB-INFもWebコンテンツの一部として含まれているという事実のようです。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

ステートレス セッション 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
}
4

3 に答える 3

7

同じ例外を受け取りました。問題は、2 つpersistence.xmlが OpenEJB のクラスパスにあることでした。

それらの1つを削除すると、問題は解決しました。

例外メッセージは、ここでは OpenEJB の一部では役に立ちませんが、org.apache.openejb.config.AppmodulehasFailures()またはhasErrors()メソッドをデバッグすると、呼び出しでより有用な情報を確認できEjbModule.getValidation()ます! (バージョン 4.0.0)。

于 2012-07-10T15:25:44.800 に答える
3

以下に示すように、Maven は Web プロジェクトのディレクトリ構造を作成します。

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

この問題を解決するには、ディレクトリ [resources] の下にディレクトリ [META-INF] を作成し、そこに persistence.xml を配置する必要があります。

于 2015-01-17T13:31:27.743 に答える
2

The main problem here is that while running junit, JUnit starts to think the WAR file as JAR file hence tries to find the persistance.xml in META-INF at root, while in WAR file the META-INF gets created inside WebContent folder. I simply created a symbolic link in the src folder pointing to the original META-INF. And voilla...............it worked. :)

于 2013-05-07T13:19:53.730 に答える