久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何注入同一接口的多個模擬

How to inject multiple mocks of the same interface(如何注入同一接口的多個模擬)
本文介紹了如何注入同一接口的多個模擬的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想測試的 Java 類(稱為 ServiceCaller)有這個:

The Java class (called ServiceCaller) I wish to test has this:

@Autowired @Qualifier(value="serviceA")
SomeService serviceA;

@Autowired @Qualifier(value="serviceB")
SomeService serviceB;

(有一個 doWork() 方法將檢查條件并調用 A 或 B).

(there's a doWork() method that will check a condition and call either A or B).

如何將每個服務的模擬注入到適當的變量中?

How do I inject a mock of each service into the appropriate variable?

我的 Junit 有這個:

@InjectMocks ServiceCaller classUnderTest = new ServiceCaller();

@Mock SomeService mockServiceA;
@Mock SomeService mockServiceB;

然而,當我運行測試以檢查在正確條件下調用的服務 A/B 時,我得到空指針,因為尚未注入模擬.

Yet when I run my tests to check that service A/B called under the correct condition, I get null pointers as the mock hasn't been injected.

顯然是因為對同一接口 (SomeService) 的多個依賴項.有沒有辦法在聲明模擬服務時指定限定符?還是我需要為依賴項設置設置器并設置老式方式?

Obviously its because of multiple dependencies on the same interface (SomeService). Is there a way to specify the qualifier when declaring the mock service? Or do I need to have setters for the dependencies and set the old fashioned way?

推薦答案

將你的 mocks 命名為 serviceA 和 serviceB 就足夠了.來自 Mockito 文檔:

It should be enough to name your mocks serviceA and serviceB. From Mockito documentation:

屬性設置器注入;模擬將首先按類型解析,然后,如果有多個相同類型的屬性,則通過匹配屬性名稱和模擬名稱.

Property setter injection; mocks will first be resolved by type, then, if there is several property of the same type, by the match of the property name and the mock name.

在你的例子中:

@InjectMocks ServiceCaller classUnderTest;

@Mock SomeService serviceA;
@Mock SomeService serviceB;

請注意,使用@InjectMocks 時無需手動創建類實例.

Note that it is not necessary to manually create class instance when using @InjectMocks.

盡管如此,我個人更喜歡使用構造函數注入依賴項.它使在測試中注入模擬變得更容易(只需使用模擬調用構造函數 - 無需反射工具或 @InjectMocks (這很有用,但隱藏了某些方面)).此外使用 TDD 可以清楚地看到測試類需要哪些依賴項,并且 IDE 可以生成您的構造函數存根.

Nevertheless I personally prefer injecting dependencies using constructor. It makes it easier to inject mocks in tests (just call a constructor with your mocks - without reflections tools or @InjectMocks (which is useful, but hides some aspects)). In addition using TDD it is clearly visible what dependencies are needed for the tested class and also IDE can generate your constructor stubs.

Spring Framework 完全支持構造函數注入:

Spring Framework completely supports constructor injection:

@Bean
public class ServiceCaller {
    private final SomeService serviceA;
    private final SomeService serviceB;

    @Autowired
    public ServiceCaller(@Qualifier("serviceA") SomeService serviceA,
                         @Qualifier("serviceB") SomeService serviceB) { ... }

    ...
}

可以使用以下代碼測試此代碼:

This code can be tested with:

@Mock SomeService serviceA;
@Mock SomeService serviceB;

//in a setup or test method
ServiceCaller classUnderTest = new ServiceCaller(serviceA, serviceB); 

這篇關于如何注入同一接口的多個模擬的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 欧美一级淫片免费视频黄 | 成年人免费看片 | 日韩免费观看 | 午夜影院在线观看视频 | 久久99久久久 | 日韩成人高清 | 国产精品伦理一区 | 日韩久久久 | 蜜桃视频一区二区 | 欧美激情综合 | 91av免费在线观看 | 国产一区二区在线播放 | 日本国产在线观看 | 伊人成人在线视频 | av大片在线观看 | 色播亚洲 | 青青草视频污 | 香蕉av在线 | 亚洲精品大片 | 久久在线精品 | 欧美大白屁股 | 日本黄a三级三级三级 | 一级做a爱片性色毛片 | 黄色高潮视频 | 高清一区二区三区 | av免费网站 | 午夜理伦三级理论 | 日韩精品成人 | 日韩精品久久久久 | www色| 日韩三级在线播放 | 亚洲综合在线播放 | www国产亚洲精品久久网站 | 好吊视频一区二区三区四区 | 中文字幕日韩高清 | 黄网站免费大全入口 | 日韩av免费 | 青青草视频网站 | 在线观看的av网站 | 青青草免费在线视频 | 亚洲天堂免费视频 |