考虑以下(简化)枚举: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