2015-01-26 250 views
0

我試圖在RESTful應用程序上測試一些服務,但是我找不到我缺少什麼依賴項以便正確執行JerseyTest。我使用maven所以POM文件如下:JerseyTest缺少依賴關係com.google.common.collect.Sets

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>PizzaApp</groupId> 
    <artifactId>PizzaApp</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>7.0</version> 
     </dependency> 

     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>7.0</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.5.Final</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.5.Final</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-spatial</artifactId> 
      <version>4.3</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.18.3</version> 
     </dependency> 

     <!-- Test API ======================================== --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.core</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>2.14</version> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.test-framework.providers</groupId> 
      <artifactId>jersey-test-framework-provider-grizzly2</artifactId> 
      <version>2.15</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>18.0</version> 
      <scope>test</scope> 
     </dependency> 


    </dependencies> 

    <repositories> 
     <repository> 
      <id>OSGEO GeoTools repo</id> 
      <url>http://download.osgeo.org/webdav/geotools</url> 
     </repository> 
     <repository> 
      <id>Hibernate Spatial repo</id> 
      <url>http://www.hibernatespatial.org/repository</url> 
     </repository> 
    </repositories> 

    <build> 
     <finalName>PizzaApp</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.5</version> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

我部署在GlassFish應用和使用PostgreSQL trought與空間擴展冬眠。如果我部署應用程序,一切正常,只有我無法運行的測試。

- 編輯 -

一個簡單的測試類如下:

public class PizzeriaServiceControllerTest extends JerseyTest { 

    @Override 
    public Application configure() { 
     ResourceConfig resource = new ResourceConfig(PizzeriaController.class); 
     return resource; 
    } 

    @Test 
    public void testAll() throws Exception { 
     String pizzarias = target("pizzeria/all").request().get(String.class); 
     assertEquals("[]", pizzarias); 
    } 
} 

運行測試時,我得到的錯誤是:

java.lang.NoSuchMethodError: org.glassfish.jersey.server.ApplicationHandler.<init>(Ljavax/ws/rs/core/Application;Lorg/glassfish/hk2/utilities/Binder;)V 
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:331) 
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:141) 
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:82) 
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:66) 
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:130) 
    at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277) 
    at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
+1

當您嘗試運行JerseyTest類時沒有找到類嗎?你可以發佈樣本單元測試嗎? – 2015-01-26 23:30:45

+0

@ChrisHinshaw我用請求信息編輯了問題 – tutomso 2015-02-05 11:50:21

回答

2

最好查看層次結構中所需的依賴關係的方法是運行mvn dependency:tree。這將列出您的項目所需的依賴關係。這在pom編輯器的eclipse中也是可用的。您應該對Google集合有依賴性。您可以追蹤哪個軟件包具有此依賴關係。我敢打賭,這是球衣番石榴所必需的。如果你不需要它,你可以刪除它。

或者,您可以添加google收藏。

<dependency> 
    <groupId>com.google.collections</groupId> 
    <artifactId>google-collections</artifactId> 
    <version>1.0</version> 
</dependency> 
+0

我正在使用依賴jersey-guava。我從com.google版本18.0更改爲番石榴。這解決了一個缺少類錯誤,但我仍然有問題來運行測試。我在問題中添加了錯誤。 – tutomso 2015-02-05 11:53:30