如何编写可以看到我的EJB服务的JUnit测试?

编程入门 行业动态 更新时间:2024-10-28 00:26:41
本文介绍了如何编写可以看到我的EJB服务的JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个在WebSphere 7上运行的Java EE应用程序(使用IBM RAD 7.5 / Eclipse)。

它基本上是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测试?

本文发布于:2023-11-16 18:58:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以看到   测试   EJB   JUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!