Consider the following (simplified) enumeration:
MyEnum {
ONE public int myMethod() {
// Some complex stuff
return 1;
},
TWO public int myMethod() {
// Some complex stuff
return 2;
};
public abstract int myMethod();
}
This is used in a function like:
void consumer() {
for (MyEnum n : MyEnum.values()) {
n.myMethod();
}
}
I'd now like to write a unit test for consumer
that mocks out the calls to myMethod() in each of the enumeration instances. I've tried the following:
@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()
}
But the real implementations of ONE.myMethod()
and TWO.myMethod()
are being called.
What have I done wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…