如何编写可以看到我的EJB服务的JUnit测试? [英] how to write a JUnit test that can see my EJB service?
问题描述
它基本上是user.JSP - > UserServlet - > @Stateless UserService - > UserDAO - > Oracle
我想为UserService编写一些JUnit测试。
我启动来自Eclipse的应用程序正在WebSphere上运行。我在Eclipse中的一个新的Java项目中进行了一个JUnit测试。我的问题是,我的JUnit测试如何访问UserService的引用?
有没有办法让我像@EJB注释一样做:
public class UserTests扩展TestCase {
@EJB
private UserServiceLocal userService;
public void test1()throws Exception {
Assert.assertTrue(userService is not null,(userService!= null));
}
}
这是我的UserService的基本版本:
@Stateless
public class UserService implements UserServiceLocal {
@EJB
私人UserDAOLocal userDAO;
/ * ... * /
}
注意:@EJB不注入任何东西。
注意: userService =(UserServiceLocal)new InitiatContext()。lookup(UserServiceLocal.JNDI) / code>也不行,说:NamingManager.getURLContext找不到此方案的工厂:ejblocal
@Local
public class interface UserServiceLocal {
public static final String JNDI =ejblocal:com.myapp.service.UserServiceLocal;
/ * ... * /
}
任何建议非常感谢!
Rob
那天,我的解决方案是我向EJB添加了一个远程EJB接口,我想从JUnit测试。
我创建了一个新的应用程序客户端项目,并将我的JUnit在那里测试我的JUnit测试执行EJB的远程查找。似乎可以正常工作。
但是,使用WebSphere 7时,我不得不使用createEJBStubs命令生成所需的存根。更多细节在这里和这里:
http://www-01.ibm.com/support/docview.wss?uid=swg21393419
I have a Java EE application running on WebSphere 7 (using IBM RAD 7.5 / Eclipse).
It's basically user.JSP -> UserServlet -> @Stateless UserService -> UserDAO -> Oracle
I want to write some JUnit tests for the UserService.
I launch the application from within Eclipse, it's running on WebSphere. I have a JUnit test in a new Java project in Eclipse. My question is, how can my JUnit test access a reference to UserService?
Is there a way for me to do something like with an @EJB annotation:
public class UserTests extends TestCase {
@EJB
private UserServiceLocal userService;
public void test1() throws Exception {
Assert.assertTrue("userService is not null", (userService != null));
}
}
Here's a basic version of my UserService:
@Stateless
public class UserService implements UserServiceLocal {
@EJB
private UserDAOLocal userDAO;
/* ... */
}
NOTE: @EJB doesn't inject anything.
NOTE: userService = (UserServiceLocal)new InitiatContext().lookup(UserServiceLocal.JNDI);
doesn't work either, says, "NamingManager.getURLContext cannot find the factory for this scheme: ejblocal"
@Local
public class interface UserServiceLocal {
public static final String JNDI = "ejblocal:com.myapp.service.UserServiceLocal";
/* ... */
}
Any suggestions are greatly appreciated!
Rob
At the end of the day, my solution was I added a remote EJB interface to the EJBs I want to test from JUnit.
I created a new "Application Client Project" and put my JUnit tests in there. My JUnit tests do a remote lookup of the EJB. Seems to work okay.
However, using WebSphere 7, I had to use the createEJBStubs command to generate the required stubs. More details here and here:
http://www-01.ibm.com/support/docview.wss?uid=swg21393419
这篇关于如何编写可以看到我的EJB服务的JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!