考慮以下(簡化)枚舉:powermockito:如何嘲笑抽象方法枚舉
MyEnum {
ONE public int myMethod() {
// Some complex stuff
return 1;
},
TWO public int myMethod() {
// Some complex stuff
return 2;
};
public abstract int myMethod();
}
這是在一個函數中使用,如:
void consumer() {
for (MyEnum n : MyEnum.values()) {
n.myMethod();
}
}
我現在想寫一個單元測試consumer
嘲笑每個枚舉實例中對myMethod()的調用。我已經試過如下:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyEnum.class)
public class MyTestClass {
@Test
public void test() throws Exception {
mockStatic(MyEnum.class);
when(MyEnum.ONE.myMethod()).thenReturn(10);
when(MyEnum.TWO.myMethod()).thenReturn(20);
// Now call consumer()
}
但ONE.myMethod()
和TWO.myMethod()
真正的實現都被調用。
我做錯了什麼?
我不認爲這是可能的。 – 2017-07-31 11:18:31