powermock series 3 - Suppress Un

2018-07-24  本文已影响0人  尖头核桃

Suppressing Unwanted Behavior

Quick summary

  1. Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case.

  2. 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.

  3. Use the Whitebox.newInstance(ClassWithEvilConstructor.class) method to instantiate a class without invoking the constructor what so ever.

  4. Use the @SuppressStaticInitializationFor("org.mycompany.ClassWithEvilStaticInitializer")annotation to remove the static initializer for the the org.mycompany.ClassWithEvilStaticInitializer

    class.

  5. 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.

  6. 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:

上一篇 下一篇

猜你喜欢

热点阅读