2

对于一些小型 JBoss 企业应用程序的测试,我想使用 JUnit 和Maven Cargo 插件。(我知道还有 JSFUnit,但首先我想仔细看看 Cargo。)

是否有一个简单的在线示例,我可以将其用作运行 JUnit 测试的参考,该测试使用 Maven Cargo 插件使用 JBoss(4.2 或 5.1)调用 EJB 操作?我找到了一些很好的配置介绍,但是我在 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 位于 EJB jar 根文件夹中,包含以下几行:

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;
    }
}

我还测试了一个 Web 应用程序,它可以毫无问题地调用 EJB - 两种方式,使用 InitialContext 或注释。我在部署 Web 应用程序时收到的警告是

WARN [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 信息

在两个不同的构建系统上使用 JBoss 5.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 的版本和 containerId

我已将资源文件夹移动到测试文件夹

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中的datasource配置不起作用,所以需要单独部署datasource文件。从 JBoss 的角度来看,它必须是放置在主部署目录中的文件(因为 Tomcat 位于 META-INF 文件夹中)。第二个任务是在 cargo 运行之前复制 jdbc 库。

Carlos Sanchez 博客中的这篇文章对您来说是一个很好的资源。也许您也可以使用 Selenium 代替 JSFUnit :)

要复制数据源文件,我使用以下配置:

        <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 回答