gpt4 book ai didi

java - 针对 Java EE 6 API 进行测试

转载 作者:IT老高 更新时间:2023-10-28 20:33:52 26 4
gpt4 key购买 nike

我为 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: 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.

我尝试使用 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>

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

解决方案

provider (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>

最佳答案

不确定这是否能解决您的问题,但 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 工件很重要。

关于java - 针对 Java EE 6 API 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3424207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com