针对 Java EE 6 API 进行测试

发布于 2024-09-13 17:28:06 字数 2535 浏览 5 评论 0原文

我为 JAX-RS 编写了一个补充,并将 Java EE 6 API 作为 Maven 依赖项包含在内。

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

然后我有一个小测试用例:

  @Test
  public void testIsWriteable() {
    class SpecialViewable extends Viewable {
      public SpecialViewable() {
        super("test");
      }
    }
    FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null);
    assertTrue(processor.isWriteable(SpecialViewable.class, null, null,
            MediaType.WILDCARD_TYPE));
  }

但我收到一个错误:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType
    ...

如果我将 Jersey 作为 JAX-RS 实现而不是 Java EE API 包含在内,一切都很好。

感谢 BalusC 的提示,我知道了我的猜测:Java EE 6 只是一个没有方法体的 API: 来自 java.net 博客

你可以用这个编译你的代码 jar,但当然你不能运行 您的应用程序,因为它 仅包含 Java EE 5 API 和 不包含任何方法体。如果 你尝试跑步,你会得到这个 例外:

线程“main”中出现异常 java.lang.ClassFormatError:不存在 方法中的代码属性不是 类文件中的本机或抽象 javax/邮件/会话

为了执行 Java EE 5 应用程序,您仍然需要 Java EE 5 容器,例如 GlassFish 应用服务器。

我尝试将 Jersy 添加到 test 范围,但没有成功。

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey-version}</version>
    <scope>test</scope>
</dependency>

如何测试仅依赖于官方 Java EE API 的软件?

解决方案

提供者 (Jersey) 需要放置在 pom.xml 中的 API (javeee-api) 之前。

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey-version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

I write an addition to JAX-RS and included the Java EE 6 API as a Maven dependency.

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

Then I have a little test case:

  @Test
  public void testIsWriteable() {
    class SpecialViewable extends Viewable {
      public SpecialViewable() {
        super("test");
      }
    }
    FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null);
    assertTrue(processor.isWriteable(SpecialViewable.class, null, null,
            MediaType.WILDCARD_TYPE));
  }

But I get an error:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType
    ...

If I include Jersey as a JAX-RS implementation instead of the Java EE API everything is fine.

Thanks to BalusC's hint I know what I had guessed: Java EE 6 is only an API without method bodies:
From the java.net blog

You can compile you code with this
jar, but of course you cannnot run
your application with it since it
contains only the Java EE 5 APIs and
does not contain any method bodies. If
you try to run, you would get this
exception:

Exception in thread "main"
java.lang.ClassFormatError: Absent
Code attribute in method that is not
native or abstract in class file
javax/mail/Session

In order to execute a Java EE 5
application, you'll still need a Java
EE 5 container, like for example the
GlassFish application server.

I've tried to add Jersy with test scope but it didn't work.

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey-version}</version>
    <scope>test</scope>
</dependency>

How can I test software that depends only on the official Java EE API?

Solution

The provider (Jersey) needs to be placed before the API (javeee-api) in the pom.xml.

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey-version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

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

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

发布评论

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

评论(3

梦明 2024-09-20 17:28:06

不确定这是否能解决您的问题,但 GlassFish Embedded 提供了 Java EE 6 实现。将其添加到您的 pom.xml 中:

<project>
  ...
  <repositories>
    <repository>
      <id>glassfish-extras-repository</id>
      <url>http://download.java.net/maven/glassfish/org/glassfish/extras</url>
    </repository>
  </repositories>
  ...
  <dependencies>
    <dependency>
      <groupId>org.glassfish.extras</groupId>
      <artifactId>glassfish-embedded-all</artifactId>
      <version>3.0.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    ...
  </dependencies>
  ...
</project>

javaee-api之前声明 glassfish-embedded-all 工件非常重要代码>.

Not sure this will solve your problem but GlassFish Embedded provides a Java EE 6 implementation. Add this to your pom.xml:

<project>
  ...
  <repositories>
    <repository>
      <id>glassfish-extras-repository</id>
      <url>http://download.java.net/maven/glassfish/org/glassfish/extras</url>
    </repository>
  </repositories>
  ...
  <dependencies>
    <dependency>
      <groupId>org.glassfish.extras</groupId>
      <artifactId>glassfish-embedded-all</artifactId>
      <version>3.0.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    ...
  </dependencies>
  ...
</project>

It's important to declare the glassfish-embedded-all artifact before the javaee-api.

为你鎻心 2024-09-20 17:28:06

对于我来说,JBoss 的实现比整个 Glassfish 小,所以我使用:

    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-6.0</artifactId>
        <version>${version.jboss-javaee-6.0}</version>
        <type>pom</type>
    </dependency>

test 应该也没有什么坏处。

As for me, JBoss' implementation is smaller than the whole Glassfish, so I'm using:

    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-6.0</artifactId>
        <version>${version.jboss-javaee-6.0}</version>
        <type>pom</type>
    </dependency>

<scope>test</scope> should also do no harm.

っ左 2024-09-20 17:28:06

与 JSR 提供​​程序无关的另一种选择是,

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>jsr311-api</artifactId>
  <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

这允许您将 Jersey 与不同的提供程序交换。对于 Glassfish 3.1.2,它使用 jersey-server 1.11 ,根据球衣 pom,它使用 jsr311 版本 1.1。

An alternative that is JSR provider agnostic is

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>jsr311-api</artifactId>
  <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

This allows you to swap Jersey with a different provider. For Glassfish 3.1.2, it uses jersey-server 1.11, which uses jsr311 version 1.1 according to the jersey pom.

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