Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

Testing against Java EE 6 API

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...