使用 Maven 2 和 Glassfish 3 对 EJB 进行单元测试

发布于 2024-09-13 04:51:18 字数 2040 浏览 2 评论 0原文

我一直在尝试设置我的应用程序,以便我可以整天对其 EJB 进行单元测试,但我似乎无法解决看似非常简单的问题。

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

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)

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

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

Not 和可用于运行测试的实现。我很确定测试在尝试执行时在标有 @BeforeClass 的方法中失败

container = EJBContainer.createEJBContainer();

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

<dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

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

那么,是否存在 Nexus 能够索引的 Maven 存储库来为我提供 glassfish-embedded-all 工件?我读过的几篇文章提到,现在使用的正确工件是 javax.ejb,但我没有再幸运地找到它。

正如您可能已经猜到的那样,我对单元测试完全陌生,而且总体来说对 JEE6 也相当陌生;这是否是进行单元测试 EJB 的正确方法?

I've been trying to set my application up so that I can unit test it's EJBs all day but I can't seem to get past what seems like a really simple problem.

I have a standard Maven web application set up in NetBeans 6.9. I've automatically generated a unit test for one of the EJBs but whenever I go to run it I get the error message:

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)

I've researched this one to death and I'm pretty sure the problem is that my pom currently points to a jar which contains only the API

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

Not and implementation that can be used for running the tests. I'm pretty sure the test is failing in the method marked with @BeforeClass when it tries to execute

container = EJBContainer.createEJBContainer();

The standard recommended solution is to add the glassfish-embedded-all artifact as the first project dependency with test scope

<dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

I can find a Maven package for this artifact here: http://download.java.net/maven/glassfish/ but Nexus doesn't recognize this directory or any of the subdirectories as Maven 2 repositories. I suppose I could download the jar and manually install it into Nexus but that seems to defeat the who point of installing Nexus.

So, is there a Maven repository that Nexus will be able to index to give me the glassfish-embedded-all artifact? A couple of posts I read mentioned that the correct artifact to now be using is javax.ejb but I've had no more luck finding this.

As you've probably guessed I'm completely new to unit testing and fairly new to JEE6 in general; is this even the correct way to go about unit testing EJBs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

栀子花开つ 2024-09-20 04:51:18

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

事实上,您需要像 glassfish-embedded-all这样的实现glassfish-embedded-web 如果您只使用 Web 配置文件,这似乎就是您的情况(顺便说一下,我不知道 Web 配置文件提供了 EJBContainer)。

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

那么,是否存在 Nexus 能够索引的 Maven 存储库来为我提供 glassfish-embedded-all 工件?

我无法使用 http://download.java.net/maven/glassfish/ 但 JBoss Nexus 存储库似乎确实有它(可能是因为他们在 Arquillian 中使用它):

<repository>
  <id>jboss-public-repository-group</id>
  <name>JBoss Public Maven Repository Group</name>
  <url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>

这是否是进行单元测试 EJB 的正确方法?

单元测试通常是在容器外部独立完成的(使用 Mocking 框架),所以我不会称之为单元测试。但对于集成/功能测试(容器内),EJBContainer API 确实很棒并且非常好。

另请参阅

(...) The standard recommended solution is to add the glassfish-embedded-all artifact as the first project dependency with test scope

Indeed, you need an implementation like glassfish-embedded-all or glassfish-embedded-web if you're only using the web profile, which seems to be your case (I didn't know that the web profile was providing EJBContainer by the way).

And to be precise, this artifact doesn't have to be the "first" dependency but it must be declared before the javaee-api artifact.

So, is there a Maven repository that Nexus will be able to index to give me the glassfish-embedded-all artifact?

I couldn't reproduce the issue with http://download.java.net/maven/glassfish/ but it appears that JBoss Nexus repository does have it (probably because they use it in Arquillian):

<repository>
  <id>jboss-public-repository-group</id>
  <name>JBoss Public Maven Repository Group</name>
  <url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>

is this even the correct way to go about unit testing EJBs?

Unit tests are typically done outside-container and in isolation (using a Mocking framework) so I wouldn't call that unit testing. But for integration/functional testing (in-container), the EJBContainer API is really great and perfectly fine.

See also

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