Glassfish和JPA的JUnit [英] JUnit with Glassfish and JPA

查看:112
本文介绍了Glassfish和JPA的JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Glassfish v3中运行的web应用程序。它是通过JSF v2和JPA实现的(因此有一个persistence.xml被声明为JTA数据源)。

如果我尝试使用JUnit测试我的存储库,它失败了查找,并给我这个错误:

  javax.naming.NamingException:查找失败'java:comp / env / persistence / em'in SerialContext [myEnv = 
java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.url.pkgs = com.sun .enterprise.naming,
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl}
[根异常是javax.naming.NamingException:调用异常:得到空ComponentInvocation]

它似乎要求一个transaction-type =RESOURCE_LOCAL因为它会和Glassfish的transaction-type =JTA发生冲突。

所以,我想问的是,如果可以的话,找到一种方法来运行JUnit没有[强烈]改变我的webapp的配置。
Thanks,
AN

解决方案

对于真正的容器内测试,您应该看看 Arquillian 。它允许你在容器中运行你的单元测试。

您应该查看 http:// arquillian.org/guides/ 以及GitHub上展示的 https://github.com/上的展示的Arquillian /的Arquillian-展示/ 。还有一个与JSF相关的展示。



关于你的配置。我强烈建议以这种方式配置您的项目,以便您可以使用与生产中不同的配置。



如果您只需要一个可用的JPA环境进行测试,那么您应该执行以下操作:


  • 使用 transaction-type =RESOURCE_LOCAL创建第二个JPA配置

  • 将实体管理器的setter添加到bean中。

  • 在您的测试设置中创建实体管理器可以在独立的Java应用程序中执行。

  • 尝试使用模拟框架,如 Mockito 可以模拟应用程序中不属于当前测试但需要测试的所有其他部分。


    第二种方法取决于您的架构以及它为您提供的可能性。它允许你编写非常细致的单元测试。第一种方法非常有用,可以测试应用程序在容器中的真实行为。


    I've a web application that runs in Glassfish v3. It's realized with JSF v2 and JPA (so there's a persistence.xml where is declared a JTA-data-source).

    If i try to test my repositories with JUnit, it fails the lookup and gives me this error:

    javax.naming.NamingException: Lookup failed for 'java:comp/env/persistence/em' in SerialContext[myEnv=
    java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
    java.naming.factory.url.pkgs=com.sun.enterprise.naming, 
    java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} 
    [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
    

    It seems to ask for a transaction-type="RESOURCE_LOCAL" that i can't provide it, since it'd be in conflict with Glassfish's transaction-type="JTA".

    So, what i'd like to ask is if it's possible to find a way to run JUnit without [strongly] change my webapp's configuration. Thanks, AN

    解决方案

    For real in-container tests you should have a look at Arquillian. It allows you to run your unit tests within the container.

    You should have a look at the documentation at http://arquillian.org/guides/ and the showcases at GitHub at https://github.com/arquillian/arquillian-showcase/. There is also a JSF related showcase.

    Regarding your configuration. I would strongly suggest to configure your project in such a way, that you can use a different configuration as in production.

    If you need only a working JPA environment for your tests, then you should do the following:

    • Create a second JPA configuration with transaction-type="RESOURCE_LOCAL".
    • Add a setter for the entity manager to your beans.
    • Create the entity manager within your test setup as you would do it in an standalone Java application.
    • Inject the entity manager manually in the beans.
    • Try to use a mocking framework like Mockito to mock all other parts of the application which are not a part of the current test but required for the test.

    The second approach depends on your architecture and the possibilities it offers to you. It allows you to write very fine-grained unit tests. The first approach is very usefull to test the real behaviour of your application in the container.

    这篇关于Glassfish和JPA的JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆