2

いくつかの小さなJBossエンタープライズアプリのテストには、JUnitとMavenCargoプラグインを使用したいと思います。(JSFUnitもあることは知っていますが、まずはCargoについて詳しく見ていきたいと思います。)

Maven Cargoプラグインを使用してJBoss(4.2または5.1)を使用してEJB操作を呼び出すJUnitテストを実行するためのリファレンスとして使用できるオンラインで利用可能な簡単な例はありますか?構成の概要をいくつか見つけましたが、EJBルックアップでエラーメッセージが表示されるので、その使用方法を確認すると便利です。

InitialContextを使用したテストコードは次のとおりです。

public void testEcho() {
   assertEquals("Echo Echo", lookupEchoBeanRemote().Echo("Echo"));
}

private EchoBeanRemote lookupEchoBeanRemote() {
    try {
        Context c = new InitialContext();
        return (EchoBeanRemote) c.lookup("EchoBean/remote");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

これにより、このエラーが発生します。

testEcho(de.betabeans.Echo2Test)  Time elapsed: 0.885 sec  <<< ERROR!
java.lang.reflect.UndeclaredThrowableException
    at $Proxy3.Echo(Unknown Source)
    at de.betabeans.Echo2Test.testEcho(Echo2Test.java:17)
Caused by: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
    at java.security.AccessController.doPrivileged(Native Method)
    at org.jboss.ejb3.security.client.SecurityActions.createSecurityContext(SecurityActions.java:657)
    at org.jboss.ejb3.security.client.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:59)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:74)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
    at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62)
    at $Proxy4.invoke(Unknown Source)
    at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
    at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
    ... 28 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.jboss.security.SecurityContextFactory.createSecurityContext(SecurityContextFactory.java:117)
    at org.jboss.security.SecurityContextFactory.createSecurityContext(SecurityContextFactory.java:76)
    at org.jboss.ejb3.security.client.SecurityActions$1.run(SecurityActions.java:662)
    ... 38 more
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/security/jacc/PolicyContextException
    at java.lang.ClassLoader.defineClass1(Native Method)

EJBアノテーションを使用する場合

@EJB(beanInterface=EchoBeanRemote.class,mappedName="EchoBean/remote")
private EchoBeanRemote newSessionBean;

public Echo3Test(String testName) {
    super(testName);
}

public void testEcho() {
   assertEquals("Echo Echo", newSessionBean.Echo("Echo"));
}

テスト結果は

testEcho(de.betabeans.Echo3Test)  Time elapsed: 0.001 sec  <<< ERROR!
java.lang.NullPointerException
    at de.betabeans.Echo3Test.testEcho(Echo3Test.java:20)

jndi.propertiesはEJBjarルート・フォルダーにあり、次の行が含まれています。

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
### The TimedSocketFactory connection timeout in milliseconds (0 == blocking) 
jnp.timeout=0
### The TimedSocketFactory read timeout in milliseconds (0 == blocking) 
jnp.sotimeout=0

Beanのソースコードは

package de.betabeans;

import javax.ejb.Remote;

@Remote
public interface EchoBeanRemote {
    String Echo(final String in);
}
package de.betabeans;

import javax.ejb.Stateless;

@Stateless
public class EchoBean implements EchoBeanRemote {
    public String Echo(final String in) {
        return in + " " + in;
    }
}

また、InitialContextまたはアノテーションを使用して、問題なくEJBを呼び出すことができるWebアプリケーションをテストしました。Webアプリケーションのデプロイで受け取った警告は

警告[MappedReferenceMetaDataResolverDeployer]未解決の参照がJBossWebMetaDataに存在します:[#web-app:AnnotatedEJBReferenceMetaData {name = de.betabeans.Echo3Servlet / echoBean、ejb-ref-type = null、link = null、ignore-dependecy = false、mapped / jndi- name = EchoBean / remote、resolved-jndi-name = null、beanInterface = interface de.betabeans.EchoBeanRemote}、#web-app:AnnotatedEJBReferenceMetaData {name = NewServlet / newSessionBean、ejb-ref-type = null、link = null、ignore -dependecy = false、mapped / jndi-name = NewSessionBean / remote、resolved-jndi-name = null、beanInterface = interface de.betabeans.NewSessionBeanRemote}] 12:26:11,770 INFO

2つの異なるビルドシステムでJBoss5.1.0.GAを使用して実行されたすべてのテスト。

完全なMavenプロジェクトをhttp://www.mikejustin.com/download/JBossSimpleEJBApp-ejb-test.zipにアップロードしました。

4

1 に答える 1

5

編集:ソースを追加した後

まず第一に、私の例は JBoss 4.2.3.GA と Cargo 1.0 で動作します。コードのリファクタリングを行いました:

pom.xml ファイル

    <groupId>de.betabeans</groupId>
    <artifactId>JBossSimpleEJBApp-ejb-test</artifactId>
    <packaging>ejb</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>JBossSimpleEJBApp-ejb JEE5 EJB Test</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
              <groupId>javax.ejb</groupId>
              <artifactId>ejb-api</artifactId>
              <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jboss-ejb3</artifactId>
            <version>4.2.3.GA</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.client</groupId>
            <artifactId>jbossall-client</artifactId>
            <version>4.2.3.GA</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
            <repository>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                   <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
                <id>repository.jboss.com</id>
                <name>Jboss Repository for Maven</name>
                <url>http://repository.jboss.com/maven2/</url>
                <layout>default</layout>
            </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <container>
                        <containerId>jboss42x</containerId>
                        <home>${jboss.home}</home>
                        <append>false</append>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>${jboss.home}/server/default</home>
                        <properties>
                            <cargo.jboss.configuration>default</cargo.jboss.configuration>
                            <cargo.rmi.port>1099</cargo.rmi.port>
                            <cargo.logging>high</cargo.logging>
                        </properties>
                    </configuration>
                    <wait>false</wait>
                </configuration>
                <executions>
                    <execution>
                        <id>start-container</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-container</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>JBossSimpleEJBApp-ejb-test</finalName>
    </build>

次のセクションで pom を変更しました。

  • 依存関係 (glasfish の ejb-api を使用)
  • リポジトリ (レイアウトのデフォルトで JBoss Maven2 を使用することをお勧めします)
  • cargo-maven2-plugin の version と containerId

resources フォルダーを test フォルダーに移動しました

jndi.propertiesファイルは (Bean ではなく) テストで使用する必要があり、次のようにするか、次のようにすることができます。

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

その他の設定ファイル (jboss.xml、MANIFEST.MF) は必要ありません。

サーバーの構成
jboss 4.2.3.GA サーバーと ejb.jar ファイルの最大の問題は、デフォルトでは動作しないことです。問題の説明と回避策については、こちらを参照してください。(これが一番大変でした。Jboss 5.0 サーバーではこの問題は存在しませんが、maven-cargo-plugin ではこのコンテナーは実験的なものです)

それ
だけです 以下に参照へのリンクをいくつか貼り付けます。それでも問題が解決しない場合は、修正したプロジェクト全体をお送りします。


私の最初の回答
Cargo と JBoss にはいくつかの問題があります。主な理由は、pom のデータソース構成が機能しないため、別のデータソース ファイルをデプロイする必要があるためです。JBoss の観点からは、メインのデプロイ ディレクトリに配置されたファイルである必要があります (Tomcat は META-INF フォルダーに配置されているため)。2 番目のタスクは、貨物の実行前に jdbc ライブラリをコピーすることです。

Carlos Sanchez blogからのこの投稿は、あなたにとって素晴らしいリソースです。たぶん、JSFUnitの代わりにSeleniumも使用できます:)

データソース ファイルをコピーするには、次の構成を使用します。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-ds-context</id>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                    <configuration>
                        <outputDirectory>${jboss.deploy-ds.dir}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/webresources/META-INF</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>context-ds.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
于 2009-11-10T13:16:08.523 に答える