我有这个EJB
@Stateless
public class HelloBean{
public String sayHello(){
return "hello";
}
}
当我使用此单元测试进行测试时:
@Test
public void testEmbeddedPersistence() throws NamingException {
assertTrue(true);
Map<String, Object> props = new HashMap<String, Object>();
props.put(EJBContainer.MODULES, new File("target/classes"));
props.put("org.glassfish.ejb.embedded.glassfish.instance.root", "./src/test/resources/glassfish-testing-domain");
ec = EJBContainer.createEJBContainer(props);
ctx = ec.getContext();
String s = "java:global/classes/HelloBean";
HelloBean helloBean = (HelloBean) ctx.lookup(s);
assertNotNull(helloBean);
assertABunchOfStuff();
ec.close();
}
一切正常。
但是当我把它改成
时@Stateless
public class HelloBean implements RemoteHello{
public String sayHello(){
return "hello";
}
}
@Remote
public interface RemoteHello{
public String sayHello();
}
@Test
public void testEmbeddedPersistence() throws NamingException {
assertTrue(true);
Map<String, Object> props = new HashMap<String, Object>();
props.put(EJBContainer.MODULES, new File("target/classes"));
props.put("org.glassfish.ejb.embedded.glassfish.instance.root", "./src/test/resources/glassfish-testing-domain");
ec = EJBContainer.createEJBContainer(props);
ctx = ec.getContext();
String s = "java:global/classes/HelloBean!com.mycompany.remoteInterface.RemoteHello";
RemoteHello remoteHello = (RemoteHello) ctx.lookup(s);
assertNotNull(remoteHello);
assertABunchOfStuff();
ec.close();
}
我得到了一个javax.naming.NamingException
最奇怪的是,当EJBContainer正在初始化时,它说:INFO:EJB5181:EJB HelloBean的可移植JNDI名称:[java:global / classes / HelloBean!com.mycompany.remoteInterface.RemoteHello,java:global / classes / HelloBean] 2012年2月10日下午3:55:22 com.sun.ejb.containers.BaseContainer initializeHome
之后不久:
错误测试: testEmbeddedPersistence(com.mycompaony.HelloBean):SerialContext中的'java:global / classes / HelloBean!com.mycompany.remoteInterface.RemoteHello'查找失败[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming .impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.url.pkgs = com.sun.enterprise.naming}
如何使用远程接口成功进行jndi查找。
由于
答案 0 :(得分:1)
远程接口不是EJB lite(EJB 3.1规范,表27)的一部分,可嵌入容器只需要提供EJB lite(EJB 3.1规范,第22.3.1节)。