使用Maven 2和Glassfish 3对EJB进行unit testing

我一直在尝试设置我的应用程序,以便我可以整天测试它的EJB,但我似乎无法通过看似简单的问题。

我在NetBeans 6.9中设置了标准的Maven Web应用程序。 我已经自动为其中一个EJB生成了unit testing,但每当我去运行它时,我都会收到错误消息:

Testcase: initializationError(com.example.ExampleTest): Caused an ERROR Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) at java.lang.ClassLoader.defineClass(ClassLoader.java:616) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 

我已经研究了这个,我很确定问题是我的pom当前指向一个只包含API的jar

  javax javaee-web-api 6.0 provided  

不是和可用于运行测试的实现。 我很确定测试在@BeforeClass标记的方法尝试执行时失败了

 container = EJBContainer.createEJBContainer(); 

标准推荐的解决方案是将glassfish-embedded-all工件添加为具有测试范围的第一个项目依赖项

  org.glassfish.extras glassfish-embedded-all 3.0.1 test  

我可以在这里找到这个工件的Maven包: http : //download.java.net/maven/glassfish/但是Nexus不能将这个目录或任何子目录识别为Maven 2存储库。 我想我可以下载jar并手动将其安装到Nexus中,但这似乎打败了安装Nexus的人。

那么,是否有一个Maven存储库,Nexus能够编制索引以便为我提供glassfish-embedded-all工件? 我读过的一些post提到现在正在使用的正确工件是javax.ejb,但我找不到更多的运气了。

你可能已经猜到我对unit testing是全新的,而且对JEE6来说还是比较新的; 这甚至是unit testingEJB的正确方法吗?

(…)标准推荐的解决方案是将glassfish-embedded-all工件添加为具有测试范围的第一个项目依赖项

实际上,如果您只使用网络配置文件,那么您需要像glassfish-embedded-allglassfish-embedded-web这样的实现,这似乎是您的情况(我不知道网络配置文件是顺便提供EJBContainer )。

确切地说,这个工件不一定是“第一”依赖,但它必须javaee-api工件之前声明。

那么,是否有一个Maven存储库,Nexus能够编制索引以便为我提供glassfish-embedded-all工件?

我无法通过http://download.java.net/maven/glassfish/重现这个问题,但似乎JBoss Nexus存储库确实拥有它(可能是因为他们在Arquillian中使用它):

  jboss-public-repository-group JBoss Public Maven Repository Group https://repository.jboss.org/nexus/content/groups/public  

这甚至是unit testingEJB的正确方法吗?

unit testing通常在容器外部进行,并且是隔离的(使用Mocking框架),因此我不会调用该unit testing。 但是对于集成/function测试(容器内), EJBContainer API真的很棒,非常好。

也可以看看

  • TOTD#128:EJBContainer.createEJBContainer:使用GlassFish v3的嵌入式EJB
  • 使用Embeddable GlassFish对EJB和JPA进行unit testing
  • 使用带或不带Maven的EJBContainer API(但使用GlassFish v3)
  • 将EJB 3.1容器嵌入到unit testing中 – 引导时间:5秒
  • unit testingEJB 3.1 …当unit testing示例为0.8秒时太长
  • Arquillian项目