Вопрос

We would like to test our REST APIs using arquillian.

We use glassfish for production and arquillian.

We already have arquillian tests for some JMS queue we expose, and that works fine, so we at least have some basics right. When we started using that setup for REST testing, at the first HTTP GET we sent on the REST URLS, we got the exception: com.sun.jersey.api.container.ContainerException: No WebApplication provider is present.

Here is our ShrinkWrap setup:

 public static WebArchive createTestArchive() {
    return ShrinkWrap.create(WebArchive.class, "test.war") // Create jar
            .addPackages(true, "<our packages, plus some dependencies>") //, "com.ocpsoft.pretty")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") // b
            .setWebXML("WEB-INF/web.xml")
            .addAsWebInfResource("WEB-INF/faces-config.xml")
            //.addAsWebInfResource(new StringAsset("<faces-config version=\"2.0\"/>"), "faces-config.xml")
            .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml");

}

I think our web.xml is relevant:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>    
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>


    <!-- Pretty faces config -->

    <filter>
        <filter-name>Pretty Filter</filter-name>
        <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
        <filter-name>Pretty Filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
        <dispatcher>ASYNC</dispatcher>
    </filter-mapping>

    <!-- disable annotation scanning by the PrettyFaces module -->
    <context-param>
        <param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>
        <param-value>none</param-value>
    </context-param>

</web-app>

EDIT: Due to the question, I have now changed to glassfish-embedded 3.1.1, and updated the question based on the new error message. We are using glassfish 3.1.2 for the main operation.

Also note that we first get a warning:

WARNING: Could not instantiate service class org.glassfish.osgicdi.impl.OSGiServiceExtension
java.lang.NoClassDefFoundError: org/osgi/framework/ServiceException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)

[..]

Caused by: java.lang.ClassNotFoundException: org.osgi.framework.ServiceException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

(but it seems it's safe to ignore that warning, see this and the bug it links to: http://www.java.net/forum/topic/glassfish/glassfish/could-not-instantiate-service-class-orgglassfishosgicdiimplosgiserviceextension?force=217)

Then the error:

SEVERE: WebModule[/test]StandardWrapper.Throwable
com.sun.jersey.api.container.ContainerException: No WebApplication provider is present
    at com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:69)
    at com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:391)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.create(ServletContainer.java:306)
Это было полезно?

Решение

Ok, with great help from the comment of Gab (would never make it without that hint!), I fixed the problem. That comment made me focus on the versions of packages, and I realized that glassfish 3.1.2, that we use for production, uses jersey 1.11, while glassfish-embedded 3.1.1, which we now use for testing (as there is no 3.1.2 as of this writing), uses jersey 1.8.

Therefore I have added the following dependencies to the TEST section of our pom.xml:

 <!-- glassfish 3.1 ships with jersey 1.8, hence the version here. -->
 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-server</artifactId>
     <version>1.8</version>
     <scope>provided</scope>
  </dependency>
  <dependency>
     <groupId>com.sun.jersey.contribs</groupId>
     <artifactId>jersey-multipart</artifactId>
     <version>1.8</version>
  </dependency> 
  <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-json</artifactId>
     <version>1.8</version>
  </dependency> 

I have left the dependencies to jersey in the main section of our pom.xml pointing towards jersey 1.11.

And now the problem is solved.

I wish we could get a better error message for such problems!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top