问题描述
这个问题是关于 Java EE 6 ,使用 glassfish v3 embedded-all 。
This questiong is regarding Java EE 6, using glassfish v3 embedded-all.
我有一个使用EJBContainer测试我的无状态EJB的单元测试。问题是我在使用JNDI查找EJB(远程)时遇到问题:
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 {
...
}
给出异常:
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资源路径:
I have tried several JNDI resource paths:
例如
java:global/BookServiceEJB
java:global/BookService
偶数:
java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB
等......
nothings有效
nothings works
我没有配置任何xml部署文件,只有持久性。 META-INF中的xml
。
测试使用的是maven surefire:
The test is using maven surefire:
mvn clean test
非常感谢任何帮助!
注意:完全部署到Glassfish服务器(使用appclient, @EJB
注入)
Note: a full deploy to Glassfish server works (using appclient, and @EJB
injection)
推荐答案
经过多次搜索,找到适合我的解决方案...
After much searching, found the solution that works for me...
您必须使用以下属性配置EJBContainer:EJBContainer.MODULES,以及模块类所在的位置(如果使用maven,'target / c lasses')。
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').
例如。
...
props = new Properties();
props.put(EJBContainer.MODULES, new File("target/classes"));
ec = EJBContainer.createEJBContainer(props);
...
如果您的EJB使用JPA,则另一个问题是您不会能够在嵌入式容器中定义数据源,因此必须使用默认的ds:'jdbc / __ default'。
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'.
所以例如我的persistence.xml看起来像这样:
so for example my persistence.xml looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="bookshelf" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.blah.domain.Book</class>
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
我还没想出如何配置嵌入式容器测试以使用一个DS(jdbc / __默认值) ),我的应用程序使用另一个(例如jdbc / booksDS)
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)
参见:
参见:
说实话我不知道为什么人们会厌烦Java EE当像春天这样的解决方案变得如此简单时......
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.
这篇关于Java EE 6 - 嵌入式容器EJB测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!