Glassfish和JPA的JUnit [英] JUnit with Glassfish and JPA
问题描述
我有一个在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环境进行测试,那么您应该执行以下操作: 第二种方法取决于您的架构以及它为您提供的可能性。它允许你编写非常细致的单元测试。第一种方法非常有用,可以测试应用程序在容器中的真实行为。 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: 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: 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屋!
transaction-type =RESOURCE_LOCAL创建第二个JPA配置
。
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 ]
transaction-type="RESOURCE_LOCAL"
.