它基本上是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命令生成所需的存根。更多细节在这里和这里:
www-01.ibm/support/docview.wss?uid=swg21393419
pic.dhe.ibm/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Frejb_3stubscmd.html
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:
www-01.ibm/support/docview.wss?uid=swg21393419
pic.dhe.ibm/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Frejb_3stubscmd.html
更多推荐
如何编写可以看到我的EJB服务的JUnit测试?
发布评论