Java EE 6 - 嵌入式容器 EJB 测试

发布于 2024-09-27 21:24:14 字数 1412 浏览 2 评论 0原文

这个问题是关于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-10-04 21:24:14

经过多次搜索,找到了适合我的解决方案...

您必须使用以下属性配置 EJBContainer:EJBContainer.MODULES 以及模块类所在的位置(如果使用 maven,则为“target/classes”)。

例如,

...
props = new Properties();
props.put(EJBContainer.MODULES, new File("target/classes"));
ec = EJBContainer.createEJBContainer(props);
...

如果您的EJB使用JPA,那么还有另一个问题,您将无法在嵌入式容器中定义数据源,因此必须使用默认的ds:'jdbc/__default'。

例如,我的 persistence.xml 看起来像这样:

<?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/__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.

...
props = new Properties();
props.put(EJBContainer.MODULES, new File("target/classes"));
ec = EJBContainer.createEJBContainer(props);
...

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:

<?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> 

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.

何必那么矫情 2024-10-04 21:24:14

您需要检查一些项目,以确保可以通过 context.lookup 加载 bean,避免 NamingException

  1. 确保你有一颗豆子。这听起来可能是显而易见的事情,但我花了很多时间试图找出为什么我无法在测试中获得我的服务实例。原因是我缺少 Stateless 注释。

  2. 正如@Dzhu指出的那样,在创建容器时添加模块。对于 maven classes 将是 target/classes,对于 maven tests 将是 target/ test-classes

  3. 如果您在控制台中发现类似于 SEVERE: EJB6005:No EJB modulefound 的消息,则说明有问题。它告诉您不存在无状态带注释的类

  4. 看看嵌入式 glassfish 控制台!在那里你会看到你的豆子的查找名称。请注意格式为 INFO: EJB5181:EJB YourBean 的可移植 JNDI 名称的消息:[java:global/classes/YourBean!bean.package.YourBean,
    java:global/classes/YourBean]
    。这意味着您可以通过调用 context.lookup("java:global/classes/YourBean!bean.package.YourBean") 或较短的名称 context.lookup(" 来查找您的 bean 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.

  1. 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.

  2. 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.

  3. 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 classes

  4. Take 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,
    java:global/classes/YourBean]
    . That means you can lookup your bean either by calling context.lookup("java:global/classes/YourBean!bean.package.YourBean") or by the shorter name context.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.

糖果控 2024-10-04 21:24:14

我编写了一个关于使用嵌入式 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文