比方說,我有以下的接口和實現:單元測試測試依賴性交互?
RequestHandler.java
public interface RequestHandler {
public Response handle(Request request);
}
RequestHandlerImpl.java
public class RequestHandlerImpl implements RequestHandler {
private HttpHandler httpHandler;
private SQLHandler sqlHandler;
public RequestHandlerImpl(HttpHandler httpHandler, SQLHandler sqlHandler) {
this.httpHandler = httpHandler;
this.sqlHandler = sqlHandler;
}
@Override
public Response handle(Request request) {
// Disassemble request and dispatch to HttpHandler and SQLHandler
// Return response based off interaction with handlers
}
}
對於這種具體實施的RequestHandler
我委託一些請求處理爲HttpHandler
和SQLHandler
。當單元測試時,我應該剔除HttpHandler
和SQLHandler
並且實際測試我的請求是否被委派給這些對象?或者,我只需要從基於合同的角度測試我的實施,即測試我的handle(Request request)
方法是否會返回有意義的響應?
Ref。 [集成測試](http://en.wikipedia.org/wiki/Integration_testing):「集成測試是軟件測試階段,其中各個軟件模塊被組合並作爲一個組進行測試,它發生在單元測試之後和驗證之前測試「。 – user2864740
@ user2864740除了這不是集成測試之外,我正在嘲笑依賴關係 –
單元測試最好每次只隔離一個類,在本例中爲'RequestHandlerImpl'。您應該注入'httpHandler'和'sqlHandler'的模擬實例,以便通過其相關的單元測試完全執行'RequestHandlerImpl' –