2016-02-06 82 views
0

我有一個使用eclipse的jetty maven插件在Jetty中運行的REST API;並使用rest-assured進行集成測試。對於一些我的IT由於連接被拒絕而一直失敗。我仔細檢查了其餘的依賴是最新的,所以現在我想知道碼頭是否真的在IT階段運行。這裏是我的代碼:使用REST Assured和Jetty進行集成測試

依賴關係:

<dependency> 
    <groupId>com.jayway.restassured</groupId> 
    <artifactId>rest-assured</artifactId> 
    <version>2.8.0</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpclient</artifactId> 
    <version>4.5.1</version> 
</dependency> 

碼頭Maven插件:

<plugin> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-maven-plugin</artifactId> 
     <version>9.2.1.v20140609</version> 
     <configuration> 
      <httpConnector> 
      <port>8103</port> 
      </httpConnector> 
      <stopPort>8005</stopPort> 
      <stopKey>STOP</stopKey> 
     </configuration> 
     <executions> 
      <execution> 
      <id>start-jetty</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>stop</goal> 
       <goal>run-war</goal> 
      </goals> 
      <configuration> 
       <scanIntervalSeconds>0</scanIntervalSeconds> 
       <daemon>true</daemon> 
      </configuration> 
      </execution> 
      <execution> 
      <id>stop-jetty</id> 
      <phase>post-integration-test</phase> 
      <goals> 
       <goal>stop</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 

IT代碼:

@Test 
    public void testHead() throws Exception { 
     //J- 
     given() 
      .port(PORT) 
      .head(URL) <-- /status 
      .then() 
      .statusCode(200); 
     //J+ 
    } 

錯誤消息我看到的是:

java.net.ConnectException: Connection refused 

    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:579) 
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117) 
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177) 
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304) 
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611) 
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446) 
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882) 
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) 
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) 
    at org.apache.http.client.HttpClient$execute$0.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) 
    at com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.doRequest(RequestSpecificationImpl.groovy:1952) 
    at com.jayway.restassured.internal.http.HTTPBuilder.doRequest(HTTPBuilder.java:492) 
    at com.jayway.restassured.internal.http.HTTPBuilder.request(HTTPBuilder.java:441) 
    at com.jayway.restassured.internal.http.HTTPBuilder$request$2.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendHttpRequest(RequestSpecificationImpl.groovy:1374) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) 
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy) 
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48) 
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:58) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:1156) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) 
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy) 
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149) 
    at com.jayway.restassured.internal.filter.SendRequestFilter.filter(SendRequestFilter.groovy:30) 
    at com.jayway.restassured.filter.Filter$filter$0.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at com.jayway.restassured.filter.Filter$filter.call(Unknown Source) 
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73) 
    at com.jayway.restassured.filter.time.TimingFilter.filter(TimingFilter.java:56) 
    at com.jayway.restassured.filter.Filter$filter.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141) 
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73) 
    at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) 
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy) 
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48) 
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:58) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:182) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159) 
    at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy) 

在我看來,碼頭根本無法啓動?這就是連接被拒絕的原因?是因爲jetty-maven-plugin和rest-assured的版本不兼容?

+0

任何解決方案?我也遇到了同樣的錯誤 –

回答

0

當您試圖連接的端口上沒有監聽服務時,通常會發生此異常。

請檢查以下內容:

  1. 服務器停機?
  2. 服務器是否正在等待/配置爲獲取連接?
  3. 試圖連接時錯誤的端口號?
相關問題