2012-12-13 23 views
6

我在使用Dropwizard運行resourceTest時遇到「Missing dependency」異常:0.6.1(球衣1.15),有沒有人有過這種情況?Dropwizard/Jersey - 在運行測試時缺少對公共方法的依賴

我的測試文件

public class MyResourceImplTest extends ResourceTest { 
    ........ 
    @Override 
    protected void setUpResources() throws Exception { 
     addResource(new MyResourceImpl(new myConfiguration())); 
    } 
} 

異常

Dec 13, 2012 2:10:41 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer <init> 
INFO: Creating low level InMemory test container configured at the base URI http://localhost:9998/ 
Dec 13, 2012 2:10:42 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer start 
INFO: Starting low level InMemory test container 
Dec 13, 2012 2:10:42 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.15 10/30/2012 02:40 PM' 
Dec 13, 2012 2:10:42 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: Missing dependency for method public javax.ws.rs.core.StreamingOutput com.****************.********(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String) at parameter at index 0 
Dec 13, 2012 2:10:42 PM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer stop 
INFO: Stopping low level InMemory test container 
+0

你知道了嗎?我喜歡用類似的東西 – Kimble

+1

是的,我的問題是,我注入了一個由InMemory容器支持的HttpServletRequest,在這種情況下,您需要使用jetty grizzlyWebTestContainer或jetty。但在一天結束時,我最終編寫了一個使用python進行集成測試來測試我的Web服務。事情變得更容易。 – Shengjie

回答

2

我的問題是,我注入了一個由InMemory容器支持的HttpServletRequest,在這種情況下,我需要使用jetty grizzlyWebTestContainer或jetty作爲測試控件。我並沒有完全理解這一點,因爲在球衣測試框架中引入灰熊確實給dropwizard本身帶來了很多依賴衝突。我認爲解決所有衝突並不值得,因爲在將來升級dropwizard時,這可能會再次發生。

在一天結束時,我結束了一些jenkins工作,使用一些集成測試(在Python中)來測試我的Web服務。例如。部署後,在一些http請求中觸發,檢查響應代碼和響應內容。事情變得更容易。

2

貌似新澤西不能注入HttpServletRequest

是配置了您的一個端點喜歡這個?

public StreamingOutput something(@Context HttpServletRequest request, String a, String b) {} 

如果是的話,你可能要重新考慮你的設計和選擇,而不是對

@Context 
private HttpContext context; 

public StreamingOutput something(String a, String b) { 

    System.out.println("Request info "+context.getRequest().getAbsolutePath()); 

} 

這可以產生一個更簡潔的方法。只要你依賴Class資源註冊,那麼你就可以保證每個請求都有一個新的實例,避免線程問題。

+0

謝謝,我注意到HttpServletRequest不能被注入。但在我的情況下,我想通過調用HttpServletRequest.getAttribute(「blabla」)來獲取請求屬性,這似乎並不是由HttpContext提供的。 – Shengjie

+0

不確定它是否會對您有所幫助,但在這裏有一些關於屬性提取的討論:http://jersey.576304.n2.nabble.com/Why-doesn-t-HttpRequestContext-expose-request-attributes-td3953625.html –

+0

我有同樣的問題。任何人已經解決了這個問題?我也在做一些blabla(@Context HttpServletRequest請求,...),它工作,但在測試中失敗。如果我使用HttpContext,那麼我需要通過servlet過濾器設置的標頭。我怎樣才能從HttpContext接口獲得這個頭部?有任何想法嗎? – heaphach

0

我解決這個問題的方法是定義一個抽象的基類資源,而不需要注入上下文,然後爲您的真實服務實現一個微小的派生類。

@Path("/contextMethod") 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public class MyResourceWithContext extends BaseResource { 
    @Context 
    private HttpServletRequest request; 

    protected String getUserID() 
    { 
     return request.getRemoteUser(); 
    } 
} 

當你運行你的測試,你再實現一個備用派生類只是用於測試,不使用HttpServletRequest的。這裏的額外好處是,派生的可測試類可以爲上下文注入(例如)硬編碼值來創建一些合適的測試場景。