针对 Java EE 6 API 进行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3424207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing against Java EE 6 API
提问by deamon
I write an addition to JAX-RS and included the Java EE 6 API as a Maven dependency.
我为 JAX-RS 编写了一个补充,并将 Java EE 6 API 作为 Maven 依赖项包含在内。
<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.
如果我将 Jersey 作为 JAX-RS 实现而不是 Java EE API 包含在内,一切都很好。
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
感谢 BalusC 的提示,我知道我的猜测:Java EE 6 只是一个没有方法主体的 API: 来自 java.net 博客
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.
您可以使用这个 jar 编译您的代码,但当然您不能使用它运行您的应用程序,因为它只包含 Java EE 5 API 并且不包含任何方法主体。如果你尝试运行,你会得到这个异常:
线程“main”中的异常 java.lang.ClassFormatError:在类文件 javax/mail/Session 中非本机或抽象的方法中缺少代码属性
为了执行 Java EE 5 应用程序,您仍然需要一个 Java EE 5 容器,例如 GlassFish 应用程序服务器。
I've tried to add Jersy with testscope but it didn't work.
我试过用test范围添加 Jersy,但它没有用。
<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?
如何测试仅依赖于官方 Java EE API 的软件?
Solution
解决方案
The provider (Jersey) needs to be placed beforethe API (javeee-api) in the pom.xml.
提供者(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>
采纳答案by Pascal Thivent
Not sure this will solve your problem but GlassFish Embedded provides a Java EE 6 implementation. Add this to your pom.xml:
不确定这会解决您的问题,但 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>
It's important to declare the glassfish-embedded-allartifact beforethe javaee-api.
据申报是很重要的glassfish-embedded-all神器之前的javaee-api。
回答by Victor Sergienko
As for me, JBoss' implementation is smaller than the whole Glassfish, so I'm using:
至于我,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>
<scope>test</scope>should also do no harm.
<scope>test</scope>也应该没有坏处。
回答by Snekse
An alternative that is JSR provider agnostic is
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>
This allows you to swap Jersey with a different provider. For Glassfish 3.1.2, it uses jersey-server1.11, which uses jsr311version 1.1 according to the jersey pom.
这允许您与不同的提供商交换 Jersey。对于 Glassfish 3.1.2,它使用jersey-server1.11,jsr311根据球衣 pom使用版本 1.1。

