Remote JNDI lookup for JUnit tests
max010 Jul 19, 2013 12:03 AMHi,
I have been trying to JUnit test some beans in my application. I have set up JBoss-eap-6.1 and junit4.10 on my Eclipse.
I have followed the instructions on https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI and https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project but I cannot get it to work.
Here is my stateless bean:
{code:java}
package myapp.ejb.account;
@Stateless
@Remote(AccountRemote.class)
public class AccountBean implements AccountRemote {
public void createUser( final UserValue user ) {
//some code
}
}
{code}
And I have a remote interface AccoutnRemote:
{code:java}
@Remote
public interface AccountRemote {
void createUser( final UserValue user );
{code}
Then in my test folder I have a class AccountUnitTest that does some testing on the AccountBean:
{code:java}
public class AccountUnitTest {
static AccountRemote accountRemote = null;
public AccountUnitTest() throws Exception {
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
// create a context passing these properties
final Context ctx = new InitialContext(jndiProps);
// lookup
Object ref = ctx.lookup("ejb:/myapp-web/AccountBean!com.myapp.ejb.account.AccountRemote");
accountRemote = (AccountRemote) PortableRemoteObject.narrow(ref, AccountRemote.class);
}
{code}
The JNDI string to lookup to get the accountRemote is composed of the name of .war deployed witch is myapp-web.war, without the .war (myapp-web).
Then the name of the bean AccountBean, then the ! and then the fully qualified name of the AccountRemote interface com.myapp.ejb.account.AccountRemote.
The standalone.xml of my JBoss instance has configured port 4447 for remoting invocation:
<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
<socket-binding name="management-native" interface="management" port="${jboss.management.native.port:9999}"/>
<socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>
<socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9443}"/>
<socket-binding name="ajp" port="8009"/>
<socket-binding name="http" port="8080"/>
<socket-binding name="https" port="8443"/>
<socket-binding name="jacorb" interface="unsecure" port="3528"/>
<socket-binding name="jacorb-ssl" interface="unsecure" port="3529"/>
<socket-binding name="messaging" port="5445"/>
<socket-binding name="messaging-throughput" port="5455"/>
<socket-binding name="osgi-http" interface="management" port="8090"/>
<socket-binding name="remoting" port="4447"/>
<socket-binding name="txn-recovery-environment" port="4712"/>
<socket-binding name="txn-status-manager" port="4713"/>
<outbound-socket-binding name="mail-smtp">
<remote-destination host="localhost" port="25"/>
</outbound-socket-binding>
</socket-binding-group>
When starting the JBoss instance I can see the JNDI bindings for the AccountBean:
14:57:08,859 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-11) JNDI bindings for session bean named AccountBean in deployment unit deployment "myapp-web.war" are as follows:
14:57:08,859 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-11) JNDI bindings for session bean named AccountBean in deployment unit deployment "myapp-web.war" are as follows:
java:global/myapp-web/AccountBean!myapp.ejb.account.AccountRemote
java:app/myapp-web/AccountBean!myapp.ejb.account.AccountRemote
java:module/AccountBean!myapp.ejb.account.AccountRemote
java:jboss/exported/myapp-web/AccountBean!myapp.ejb.account.AccountRemote
java:global/myapp-web/AccountBean
java:app/myapp-web/AccountBean
java:module/AccountBean
Yet when I try to run the AccountUnitTest as a JUnit test it fails with the error:
javax.naming.NameNotFoundException: ejb:/myapp-web/AccountBean!myapp.ejb.account.AccountRemote -- service jboss.naming.context.java.jboss.exported.ejb:.myapp-web."AccountBean!myapp.ejb.account.AccountRemote"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:103)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:197)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:174)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
While debugging in Eclipse I also get the following stack trace error:
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner at localhost:53210
Thread [main] (Suspended (exception ClassNotFoundException))
owns: Object (id=22)
owns: Object (id=23)
URLClassLoader$1.run() line: 366
URLClassLoader$1.run() line: 355
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: 354
I am not sure what is going on here. Can anyone plese help me solve this issue?
Thanks,
Max