title: Where can I find a complete Maven Cargo plugin example for EJB tests?
body: <p>For tests of some small JBoss enterprise apps I would like to use JUnit, and the <a href="http://cargo.codehaus.org/Maven2+plugin" rel="nofollow noreferrer">Maven Cargo plugin</a>. (I know that there is also JSFUnit but first I would like to take a closer look at Cargo.)</p>
  
  <p>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.</p>
  
  <p>Here is the test code using InitialContext:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Which gives this error:</p>
  
  <pre><code>testEcho(de.betabeans.Echo2Test)  Time elapsed: 0.885 sec  &lt;&lt;&lt; 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)
  </code></pre>
  
  <p>If I use the EJB annotation </p>
  
  <pre><code>@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"));
  }
  </code></pre>
  
  <p>The test result is </p>
  
  <pre><code>testEcho(de.betabeans.Echo3Test)  Time elapsed: 0.001 sec  &lt;&lt;&lt; ERROR!
  java.lang.NullPointerException
      at de.betabeans.Echo3Test.testEcho(Echo3Test.java:20)
  </code></pre>
  
  <p>jndi.properties is located in the EJB jar root folder and contains these lines:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The bean source code is</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <p>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</p>
  
  <p>All tests performed with JBoss 5.1.0.GA on two different build systems.</p>
  
  <p>I have uploaded the complete Maven project now to <a href="http://www.mikejustin.com/download/JBossSimpleEJBApp-ejb-test.zip" rel="nofollow noreferrer">http://www.mikejustin.com/download/JBossSimpleEJBApp-ejb-test.zip</a></p>