久久久久久久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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 黄色国产在线播放 | 国产精品久久久久久久久免费高清 | 中文字幕免费观看 | 毛色毛片免费看 | 日韩一级电影免费观看 | 九九九色 | 国产91黄色 | 久久久欧洲| 欧美日韩黄色一级片 | 九九亚洲 | 国产精品毛片一区二区三区 | 青青草原综合久久大伊人精品 | www.黄网 | 国产精品精品久久久 | 久久精品欧美一区二区三区不卡 | 精品视频一区二区三区在线观看 | 九九国产在线观看 | 亚洲美女视频 | 日韩精品一区二区三区中文字幕 | 日韩精品一区二区三区视频播放 | 精品一区二区久久久久久久网精 | 新超碰97| 香蕉视频一区二区 | 激情欧美日韩一区二区 | 亚洲国产欧美一区二区三区久久 | 亚洲 欧美 精品 | 国产真实乱全部视频 | 欧美成人a | 成人在线视频观看 | 成人a免费 | 久久久久亚洲精品中文字幕 | 国产精品国产三级国产aⅴ原创 | 久久久久久久久淑女av国产精品 | 亚洲欧美精 | 又黑又粗又长的欧美一区 | a a毛片 | h视频免费在线观看 | 91久久久久久久 | 日韩国产黄色片 | 日韩欧美在线免费观看视频 | 欧美午夜一区二区三区免费大片 |