Suppressing Unwanted Behavior
Quick summary
-
Use the
@RunWith(PowerMockRunner.class)
annotation at the class-level of the test case. -
Use the
@PrepareForTest(ClassWithEvilParentConstructor.class)
annotation at the class-level of the test case in combination with
suppress(constructor(EvilParent.class))
to suppress all constructors for the EvilParent class. -
Use the
Whitebox.newInstance(ClassWithEvilConstructor.class)
method to instantiate a class without invoking the constructor what so ever. -
Use the
@SuppressStaticInitializationFor("org.mycompany.ClassWithEvilStaticInitializer")
annotation to remove the static initializer for the theorg.mycompany.ClassWithEvilStaticInitializer
class.
-
Use the
@PrepareForTest(ClassWithEvilMethod.class)
annotation at the class-level of the test case in combination with
suppress(method(ClassWithEvilMethod.class, "methodName"))
to suppress the method with name "methodName" in the ClassWithEvilMethod class. -
Use the
@PrepareForTest(ClassWithEvilField.class)
annotation at the class-level of the test case in combination with
suppress(field(ClassWithEvilField.class, "fieldName"))
to suppress the field with name "fieldName" in the ClassWithEvilField class.
You'll find the member modification and member matcher methods here:
org.powermock.api.support.membermodification.MemberModifier
org.powermock.api.support.membermodification.MemberMatcher
网友评论