[英]Powermock: How to mock a method in an enumeration constant
我有一個枚舉類型,我將其用作實現公共接口的其他對象的工廠集合。 代碼的簡化版本是:
interface Operation {
void execute();
}
enum Factory {
TYPE1(Class1::new),
TYPE2(Class2::new);
private Supplier<Operation> constructor;
Factory(Supplier<Operation> constructor) {
this.constructor = constructor;
}
Operation build() {
return constructor.get();
}
}
客戶端代碼的簡化版本如下所示:
class Client {
private void run(EnumSet<Factory> required) {
for (Factory x : required) {
x.build().execute();
}
}
// Some code that calls run() with the right EnumSet
}
這一切似乎都按預期工作,所以我想編寫一些單元測試。
測試Factory
很容易,但事實證明Client
更困難。 問題是我不想開始調用Operation
(他們做了很多工作)。 相反,我想讓x.build()
返回一個模擬對象。
我已經嘗試使用 PowerMocks whenNew
來捕獲Operation
對象的創建,但這不起作用(我實際上沒有任何“新”操作)。 我也嘗試過使用 Powermock 'spy',但是這失敗了,因為枚舉常量是真實的對象。
有任何想法嗎?
兩種解決方案:
例如像這樣:
interface OperationProvider {
Operation build();
}
像這樣使用:
enum Factory implements OperationProvider {
現在你可以改變你的客戶端代碼來做
OperationProvider ourProvider = ...
whatever.build();...
關鍵是:在使用依賴注入時,您只需傳遞一個模擬的OperationProvider。 這意味着您基本上可以完全避免使用枚舉進行模擬的需要。 例如,您只需要確保在您的生產代碼中,您顯然在初始化ourProvider
字段時傳遞了枚舉常量之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.