How do I correctly suppress a parent class method on a Spy?
If I have a class Parent:
public class Parent {
public void method() {
System.out.println("Parent.method");
}
}
class Child extends Parent {
@Override
public void method() {
super.method();
System.out.println("Child.method");
}
}
that I test with the following code:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Parent.class)
public class SuppressParentTest {
@Spy Child child = new Child();
@Test
public void testSuppressSuperclassMethods() {
PowerMockito.suppress(methodsDeclaredIn(Parent.class));
child.method();
}
I get the following printout from System.out:
Parent.method
Child.method
whereas I should only get a printout of Child.method
.
Interestingly if I delete the @Spy
annotation from the declaration of the Child object, then Parent.method()
is correctly suppressed.
Am I doing something wrong? Do I misunderstand how to use PowerMock?