美文网首页
powermock series 3 - Suppress Un

powermock series 3 - Suppress Un

作者: 尖头核桃 | 来源:发表于2018-07-24 14:42 被阅读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:

  • org.powermock.api.support.membermodification.MemberModifier
  • org.powermock.api.support.membermodification.MemberMatcher

相关文章

网友评论

      本文标题:powermock series 3 - Suppress Un

      本文链接:https://www.haomeiwen.com/subject/txulixtx.html