Search code examples
javagradlejunitjbossjndi

ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory when trying to load InitialContext


I'm testing (with JUnit) an rest service and to make sure everything goes as intended I need to use some EJB methods. Say, I have:

  • the class under test, which is of no interest here;
  • testing class
    public class UploadServiceTest {
        private final String RemoteBeanLookupKey = "/project/dao/TaskManager!ru.project.dao.TaskManager";
        @EJB private TaskManager taskManager;

        @Before
        public void startEverythingNeeded() throws Exception {
            InitialContext ctx = null;
            Properties jndiProp = new Properties();

            InputStream testConfStream = getClass().getClassLoader().getResourceAsStream("jndi.properties");
            jndiProp.load(testConfStream);

            ctx = new InitialContext(jndiProp);

            taskManager = ((TaskManager) ctx.lookup(RemoteBeanLookupKey));
        }
        @Test 
        public void blablabla(){
        }
    } 
  • jndi.properties

java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory java.naming.provider.url=http-remoting://localhost:8080 remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false jboss.naming.client.ejb.context=true

remote.connection.default.username=admin
remote.connection.default.password=admin
  • gradle dependencies: testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom', testCompile group: 'junit', name: 'junit', version: '4.11' and provided project(path: ':dao') (this is the module i want to get EJB from).

But when I try to run test, it fails with:

javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory
[Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]

Other similar questions on here and on the net suggest to add jboss-client to CLASSPATH, but I've looked into README near jboss-client in my distribution and it said not to act like this and to make a gradle dependency instead. So I did.

Another strange thing about this: I got code and properties from tests to another module in same project (written by another coder). I tried to run those tests and they work as intended. I copied everything and even more (gradle dependency), but get this exception.

I've tried to simplify the code in order to illustrate, I may have something important missing. If needed, I can copy some more parts of setup and code.


Solution

  • I changed the dependency on ejb-client from testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom' to testCompile 'org.wildfly:wildfly-ejb-client-bom:10.0.0.Final' and it started working. Not sure if it is helpfull.