Pour les tests de certaines petites applications d'entreprise JBoss, je voudrais utiliser JUnit et le Maven Cargo plugin. (Je sais qu'il y a aussi JSFUnit mais je voudrais d'abord regarder de plus près Cargo.)Où puis-je trouver un exemple complet de plug-in Maven Cargo pour les tests EJB?
Existe-t-il un exemple simple disponible en ligne que je pourrais utiliser comme référence pour exécuter un test JUnit qui appelle une opération EJB en utilisant JBoss (4.2 ou 5.1) en utilisant le plugin Maven Cargo? J'ai trouvé quelques bonnes introductions à la configuration, mais je reçois des messages d'erreur dans la recherche EJB, il serait donc utile de voir comment il devrait être utilisé.
Voici le code de test en utilisant 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);
}
}
Ce qui donne cette erreur:
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)
Si j'utilise l'annotation 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"));
}
Le résultat du test est
testEcho(de.betabeans.Echo3Test) Time elapsed: 0.001 sec <<< ERROR!
java.lang.NullPointerException
at de.betabeans.Echo3Test.testEcho(Echo3Test.java:20)
jndi.properties se trouve dans le dossier racine jar EJB et contient ces lignes:
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
Le code source de haricot est
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;
}
}
J'ai également testé une application web qui peut appeler le EJB sans problèmes - dans les deux sens, avec InitialContext ou une annotation. Un avertissement que j'ai reçu dans le déploiement de l'application Web a été
WARN [MappedReferenceMetaDataResolverDeployer] références non résolues dans JBossWebMetaData: [# web-app: AnnotatedEJBReferenceMetaData {name = de.betabeans.Echo3Servlet/echoBean, type ejb-ref = null, link = null, ignorer-dependecy = faux, mappé/jndi-name = EchoBean/distant, résolu-jndi-name = null, beanInterface = interface de.betabeans.EchoBeanRemote}, # web-app: AnnotatedEJBReferenceMetaData {name = NewServlet/newSessionBean, ejb-ref-type = null, link = null, ignore-dependecy = faux, mappé/jndi-name = NewSessionBean/distant, résolu-jndi-name = null, beanInterface = interface de.betabeans.NewSessionBeanRemote}] 12: 26: 11,770 INFO
Tous les tests effectués avec JBoss 5.1.0.GA sur deux diffèrent ent construire des systèmes.
J'ai téléchargé le projet Maven complet maintenant http://www.mikejustin.com/download/JBossSimpleEJBApp-ejb-test.zip
Il y a beaucoup d'échantillons Cargo + JBoss sur le Web par exemple http://i-proving.ca/space/Technologies/Maven/Maven+Recipes/Deploy+to+JBoss+using+Cargo ou http://www.vineetmanohar.com/2009/05/26/maven-cargo- jboss /. Mais il y a plusieurs façons d'utiliser Cargo aussi alors peut-être que vous devriez coller votre pom et l'erreur ici pour que nous puissions vous aider. Quel est le problème en réalité? –
Je reçois cette exception: javax.naming.NoInitialContextException: Impossible d'instancier la classe: org.jnp.interfaces.NamingContextFactory [L'exception racine est java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory] – mjn
Cela peut être corrigé en ajoutant jbossall- client.jar au POM, mais le je cours dans une exception classcast si j'utilise la méthode de recherche de contexte. Je n'ai pas de fichier de configuration jboss.xml et essaye maintenant de configurer l'EJB là. Si j'utilise une annotation @EJB, j'obtiens une exception de pointeur nul. – mjn