Java EE 6 - 嵌入式容器 EJB 测试
这个问题是关于Java EE 6,使用glassfish v3嵌入所有。
我有一个使用 EJBContainer 来测试我的无状态 EJB 的单元测试。问题是我在使用 JNDI 查找 EJB(远程)时遇到问题:
setup() {
ctx = EJBContainer.createEJBContainer().getContext();
}
...
test() {
BookService bookService = (BookService)ctx.lookup("java:global/BookServiceEJB!com.something.service.BookService");
...
}
@Stateless
public class BookServiceEJB implements BookService {
...
}
@Remote
public interface BookService {
...
}
给出了例外:
javax.naming.NamingException: Lookup failed for 'java:global/BookServiceEJB!com.something.service.BookService' in SerialContext [Root exception is javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found]
...
caused by: javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found
我尝试了几个 JNDI 资源路径:
例如
java:global/BookServiceEJB
java:global/BookService
甚至:
java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB
等等...
没有任何作用
我没有任何 xml 部署文件,只有META-INF中的persistence.xml
。
测试使用 Maven Surefire:
mvn clean test
非常感谢任何帮助!
注意:完全部署到 Glassfish 服务器是可行的(使用 appclient 和 @EJB
注入)
This questiong is regarding Java EE 6, using glassfish v3 embedded-all.
I have a unit test that uses EJBContainer to test my stateless EJB. Problem is I'm having trouble looking up the EJB (remote) using JNDI:
setup() {
ctx = EJBContainer.createEJBContainer().getContext();
}
...
test() {
BookService bookService = (BookService)ctx.lookup("java:global/BookServiceEJB!com.something.service.BookService");
...
}
@Stateless
public class BookServiceEJB implements BookService {
...
}
@Remote
public interface BookService {
...
}
gives the exception:
javax.naming.NamingException: Lookup failed for 'java:global/BookServiceEJB!com.something.service.BookService' in SerialContext [Root exception is javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found]
...
caused by: javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found
I have tried several JNDI resource paths:
e.g.
java:global/BookServiceEJB
java:global/BookService
even:
java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB
etc...
nothings works
I do not have any xml deployment files configured, only a persistence.xml
in META-INF.
The test is using maven surefire:
mvn clean test
Any help is greatly appreciated!
Note: a full deploy to Glassfish server works (using appclient, and @EJB
injection)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过多次搜索,找到了适合我的解决方案...
您必须使用以下属性配置 EJBContainer:EJBContainer.MODULES 以及模块类所在的位置(如果使用 maven,则为“target/classes”)。
例如,
如果您的EJB使用JPA,那么还有另一个问题,您将无法在嵌入式容器中定义数据源,因此必须使用默认的ds:'jdbc/__default'。
例如,我的 persistence.xml 看起来像这样:
我还没有弄清楚如何配置嵌入式容器测试以使用一个 DS (jdbc/__default),而我的应用程序使用另一个 DS (例如 jdbc/booksDS)
参见:http://www.mentby.com/glassfish/embedded- test-woes.html
参见:http ://forums.java.net/jive/thread.jspa?messageID=395759
说实话,我不知道为什么人们还要为 Java EE 烦恼,而像 spring 这样的解决方案要简单得多
......非常令人沮丧,浪费了很多时间......希望这会有所帮助。
After much searching, found the solution that works for me...
You'll have to configure the EJBContainer with the property: EJBContainer.MODULES, and the location where the module classes are (if using maven, 'target/classes').
e.g.
If your EJB uses JPA, theres another problem in that you will not be able to define a datasource in the embedded container, so have to use the default ds: 'jdbc/__default'.
so for example my persistence.xml looks like so:
I haven't figured out how to configure the embedded container test to use one DS (jdbc/__default), and my app to use another (e.g. jdbc/booksDS)
see: http://www.mentby.com/glassfish/embedded-testing-woes.html
see: http://forums.java.net/jive/thread.jspa?messageID=395759
To be honest I don't know why people are bothering with Java EE when solutions like spring is so much simpler...
It has been very frustrating and alot of time wasted... hope this helps.
您需要检查一些项目,以确保可以通过 context.lookup 加载 bean,避免 NamingException。
确保你有一颗豆子。这听起来可能是显而易见的事情,但我花了很多时间试图找出为什么我无法在测试中获得我的服务实例。原因是我缺少 Stateless 注释。
正如@Dzhu指出的那样,在创建容器时添加模块。对于 maven classes 将是 target/classes,对于 maven tests 将是 target/ test-classes。
如果您在控制台中发现类似于
SEVERE: EJB6005:No EJB modulefound
的消息,则说明有问题。它告诉您不存在无状态带注释的类看看嵌入式 glassfish 控制台!在那里你会看到你的豆子的查找名称。请注意格式为
INFO: EJB5181:EJB YourBean 的可移植 JNDI 名称的消息:[java:global/classes/YourBean!bean.package.YourBean,
。这意味着您可以通过调用 context.lookup("java:global/classes/YourBean!bean.package.YourBean") 或较短的名称 context.lookup(" 来查找您的 bean java:global/classes/YourBean") 如果没有名称冲突,这会很有用。java:global/classes/YourBean]
希望这对某人有帮助。如果有这个提示的话真的很有帮助。
There's a few items you need to check in order to make sure you can load the bean through the context.lookup avoiding a NamingException.
Make sure you have a bean. This may sound something obvious but I spent a lot of time trying to figure out why I wasn't able to get an instance of my service in the tests. The reason was that I was missing the Stateless annotation.
Add the module when creating the container as @Dzhu pointed out. For maven classes will be target/classes, for maven tests will be target/test-classes.
Something is wrong if you find a message like
SEVERE: EJB6005:No EJB modules found
in the console. It tells you that there are no Stateless annotated classesTake a look at the embedded glassfish console! In there you will see the lookup names for your beans. Pay attention to the messages in the format
INFO: EJB5181:Portable JNDI names for EJB YourBean: [java:global/classes/YourBean!bean.package.YourBean,
. That means you can lookup your bean either by callingjava:global/classes/YourBean]
context.lookup("java:global/classes/YourBean!bean.package.YourBean")
or by the shorter namecontext.lookup("java:global/classes/YourBean")
which can be useful if there's no name collisions.Hope this helps someone. It would have been really helpful to have had this tips.
我编写了一个关于使用嵌入式 glassfish 3.1 容器的小教程,还解决了需要不同的 persistence.xml 进行测试的问题。还修复了远程接口和 Web 服务的容器崩溃问题。您可以在 http:// /pschyska.blogspot.com/2011/06/unit-testing-ejb-31-with-netbeans-maven.html
I have written a small tutorial on using the embedded glassfish 3.1 container, also addressing the issue for needing a different persistence.xml for tests. Also fixing container crashes with remote interfaces and webservices. You can check it out at http://pschyska.blogspot.com/2011/06/unit-testing-ejb-31-with-netbeans-maven.html