针对 Java EE 6 API 进行测试
我为 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/SessionIn 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定这是否能解决您的问题,但 GlassFish Embedded 提供了 Java EE 6 实现。将其添加到您的
pom.xml
中:在
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
:It's important to declare the
glassfish-embedded-all
artifact before thejavaee-api
.对于我来说,JBoss 的实现比整个 Glassfish 小,所以我使用:
test
应该也没有什么坏处。As for me, JBoss' implementation is smaller than the whole Glassfish, so I'm using:
<scope>test</scope>
should also do no harm.与 JSR 提供程序无关的另一种选择是,
这允许您将 Jersey 与不同的提供程序交换。对于 Glassfish 3.1.2,它使用
jersey-server
1.11 ,根据球衣 pom,它使用 jsr311 版本 1.1。An alternative that is JSR provider agnostic is
This allows you to swap Jersey with a different provider. For Glassfish 3.1.2, it uses
jersey-server
1.11, which usesjsr311
version 1.1 according to the jersey pom.