いくつかの小さな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にアップロードしました。