在哪里可以找到用于 EJB 测试的完整 Maven Cargo 插件示例?
对于一些小型 JBoss 企业应用程序的测试,我想使用 JUnit 和 Maven Cargo 插件 。 (我知道还有 JSFUnit,但首先我想仔细看看 Cargo。)
网上是否有一个简单的示例,我可以将其用作运行 JUnit 测试的参考,该测试使用 JBoss 调用 EJB 操作(4.2或 5.1) 使用 Maven Cargo 插件?我发现了一些关于配置的很好的介绍,但是我在 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
The bean source code is
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 INFO
所有测试均在两个不同的构建系统上使用 JBoss 5.1.0.GA 执行。
我现在已将完整的 Maven 项目上传到 http://www.mikejustin.com /download/JBossSimpleEJBApp-ejb-test.zip
For tests of some small JBoss enterprise apps I would like to use JUnit, and the Maven Cargo plugin. (I know that there is also JSFUnit but first I would like to take a closer look at Cargo.)
Is there a simple example available online which I could use as a reference for running a JUnit test which invokes a EJB operation using JBoss (4.2 or 5.1) using the Maven Cargo plugin? I have found some good introductions to the configuration, but I get error messages in the EJB lookup so it would be helpful to see how it should be used.
Here is the test code using 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);
}
}
Which gives this error:
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)
If I use the EJB annotation
@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"));
}
The test result is
testEcho(de.betabeans.Echo3Test) Time elapsed: 0.001 sec <<< ERROR!
java.lang.NullPointerException
at de.betabeans.Echo3Test.testEcho(Echo3Test.java:20)
jndi.properties is located in the EJB jar root folder and contains these lines:
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
The bean source code is
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;
}
}
I have also tested a web application which can call the EJB without problems - in both ways, with InitialContext or an annotation. A warning which I received in the deployment of the web application was
WARN [MappedReferenceMetaDataResolverDeployer] Unresolved references exist in 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
All tests performed with JBoss 5.1.0.GA on two different build systems.
I have uploaded the complete Maven project now to http://www.mikejustin.com/download/JBossSimpleEJBApp-ejb-test.zip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:添加源后
首先 - 我的示例适用于 JBoss 4.2.3.GA 和 Cargo 1.0
我对您的代码进行了一些重构:
pom.xml 文件
我在以下部分中更改了您的 pom:
我已将资源文件夹移动到测试文件夹
jndi.properties 文件必须由测试(而不是bean)使用,并且可以作为以下或您拥有:
不需要其他配置文件(jboss.xml、MANIFEST.MF)。
服务器配置
jboss 4.2.3.GA 服务器和 ejb.jar 文件的最大问题是它默认不工作!您可以在此处找到问题的描述和解决方法。 (这是最难的事情。在 Jboss 5.0 服务器中,这个问题不存在,但在 maven-cargo-plugin 中,这个容器是实验性的)
就这样
下面我粘贴了一些参考链接,如果您仍然遇到问题,我会将我的整个修复项目发送给您。
My original answer
There are several problems with Cargo and JBoss. Main reason is that datasource configuration in pom doesn't work, so you need to deploy separate datasource file. From JBoss perspective it has to be a file placed in main deploy directory (for Tomcat is located in META-INF folder). Second task is copy jdbc library before cargo run.
Carlos Sanchez 博客 发布的这篇文章对您来说是很好的资源。也许您也可以使用 Selenium 而不是 JSFUnit :)
要复制数据源文件,我使用以下配置:
EDIT: after added sources
First of all - my example works on JBoss 4.2.3.GA and Cargo 1.0
I did some refactoring of your code:
pom.xml file
I've changed your pom in following sections:
I've moved resources folder to test folder
jndi.properties file must be use by test (not beans) and can be as follows or that you have:
The orher configuration files (jboss.xml, MANIFEST.MF) aren't necessary.
Configuration of server
The biggest problem with jboss 4.2.3.GA server and ejb.jar file is that it doesn't work by default! Description of the problem and workaround you can find here. (That was the hardest thing. In Jboss 5.0 server this problem doesn't exist but in maven-cargo-plugin this container is as experimental one)
That's all
Below I paste some links to references, If you'll have still problems I will send you my entire fixed project.
My original answer
There are several problems with Cargo and JBoss. Main reason is that datasource configuration in pom doesn't work, so you need to deploy separate datasource file. From JBoss perspective it has to be a file placed in main deploy directory (for Tomcat is located in META-INF folder). Second task is copy jdbc library before cargo run.
Great resource for you is this post from Carlos Sanchez blog. Maybe you can use Selenium instead JSFUnit too :)
To copy datasource file I use following configuration: