ClassNotFoundException:尝试加载 InitialContext 时 org.jboss.naming.remote.client.InitialContextFactory
ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory when trying to load InitialContext
我正在测试(使用 JUnit)休息服务,为了确保一切按预期进行,我需要使用一些 EJB 方法。说,我有:
- 正在测试的class,这里没有兴趣;
测试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=假
jboss.naming.client.ejb.context=真
remote.connection.default.username=admin
remote.connection.default.password=admin
gradle 依赖项:testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom'
、testCompile group: 'junit', name: 'junit', version: '4.11'
和 provided project(path: ':dao')
(这是我想从中获取 EJB 的模块)。
但是当我尝试 运行 测试时,它失败了 javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory
[Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
这里和网上的其他类似问题建议将 jboss-client 添加到 CLASSPATH,但我查看了我的发行版中 jboss-client 附近的 README,它说不要像这样行事并改为建立 gradle 依赖关系。所以我做到了。
关于此的另一件奇怪的事情:我从测试中获得代码和属性到同一项目中的另一个模块(由另一个编码器编写)。我尝试 运行 这些测试,它们按预期工作。我复制了所有内容甚至更多(gradle 依赖项),但出现此异常。
我试图简化代码以进行说明,我可能遗漏了一些重要的东西。如果需要,我可以复制更多的设置和代码。
我将对 ejb-client 的依赖从 testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom'
更改为 testCompile 'org.wildfly:wildfly-ejb-client-bom:10.0.0.Final'
,它开始工作了。不知道有没有用。
我正在测试(使用 JUnit)休息服务,为了确保一切按预期进行,我需要使用一些 EJB 方法。说,我有:
- 正在测试的class,这里没有兴趣;
测试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=假 jboss.naming.client.ejb.context=真
remote.connection.default.username=admin remote.connection.default.password=admin
gradle 依赖项:
testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom'
、testCompile group: 'junit', name: 'junit', version: '4.11'
和provided project(path: ':dao')
(这是我想从中获取 EJB 的模块)。
但是当我尝试 运行 测试时,它失败了 javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory
[Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
这里和网上的其他类似问题建议将 jboss-client 添加到 CLASSPATH,但我查看了我的发行版中 jboss-client 附近的 README,它说不要像这样行事并改为建立 gradle 依赖关系。所以我做到了。
关于此的另一件奇怪的事情:我从测试中获得代码和属性到同一项目中的另一个模块(由另一个编码器编写)。我尝试 运行 这些测试,它们按预期工作。我复制了所有内容甚至更多(gradle 依赖项),但出现此异常。
我试图简化代码以进行说明,我可能遗漏了一些重要的东西。如果需要,我可以复制更多的设置和代码。
我将对 ejb-client 的依赖从 testCompile group: 'org.wildfly', name: 'wildfly-ejb-client-bom', version: '8.2.0.Final', ext: 'pom'
更改为 testCompile 'org.wildfly:wildfly-ejb-client-bom:10.0.0.Final'
,它开始工作了。不知道有没有用。