【发布时间】:2017-07-31 10:59:52
【问题描述】:
考虑以下(简化的)枚举:
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() 的真正实现正在被调用。
我做错了什么?
【问题讨论】:
-
我认为这不可能。
标签: java mocking mockito powermock powermockito