关于EJB3.0的测试(斑竹以及各位高人请进)

07-07-26 gongqh21
环境:
JBoss4.2GA
JBoss IED

写有下面简单的stateless session bean:

======Remote 接口=========
public interface Hello {
public void hello();
}

=======Bean 类===========
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(Hello.class)
public class HelloBean implements Hello{
public void hello(){
System.out.println("hello()");
}
}

======测试代码================
public class Client {
public static void main(String args[]){
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");

InitialContext ctx;
try {
ctx = new InitialContext(props);
Hello hello = (Hello) ctx.lookup("HelloBean/remote");
hello.hello();

} catch (NamingException e) {
e.printStackTrace();
}
}
}

==================================
将jar包部署到JBoss下后,用Jsp页面测试,正常;但是,在Eclipse中运行上面的Client.java时出现异常,如下:
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at $Proxy0.hello(Unknown Source)
at org.nj.helloworld.Client.main(Client.java:19)
Caused by: java.lang.Exception: Can not make remoting client invocation due to not being connected to server.
at org.jboss.remoting.Client.invoke(Client.java:1555)
at org.jboss.remoting.Client.invoke(Client.java:530)
at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:41)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:46)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:40)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:72)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:103)
... 2 more

=========================
请问这是什么原因啊?

gongqh21
2007-07-26 23:43
已经解决,整理一下jar包就好啦

gongqh21
2007-07-26 23:53
EJB3.0声称可以“离开容器进行单元测试”,拿上面的例子来说,是否就是把HelloBean.java类当作一个POJO来测试啊?
如下:
=============================
public class HelloBeanTest {
static HelloBean helloBean = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
helloBean = new HelloBean();
}

public void testHello(){
//
}
}

=============================

或者还有其他的方式?

banq
2007-07-27 11:02
是的,参考Ejb3Unit

http://ejb3unit.sourceforge.net/