Mocking super implementation using powermock

Is there any method that makes fun of calling a super method on a class using Powermock?

For example:

public void processRetrievedData() {
  super.processRetrievedData();
}

      

anyway, to make fun of "super.processRetrievedData ()"?

Thank,

Rajan

+3


source to share


2 answers


This is a terrible idea and a dirty hack.

If it is part of your subject under test, you should never do this kind of thing, since you are testing the behavior of this whole class, not just part of it.



If you really need to do this, you can extend the class and override the processRetrievedData method so that it doesn't call super - this way you stub out that method.

0


source


While this is bad practice, sometimes you need to mock inheritance when working in an environment where there is no choice. For example, I needed to mock superclass methods in a dialog snippet in Android to isolate my unit tests. In your case, as part of your test use ...

@PrepareForTest(ChildClass.class)
public class ChildClassTest {
    @Test
    public void testMethod() {
        PowerMockito.suppress(PowerMockito.method(SuperClass.class, "processRetrievedData"))
        // Run method and test
    }
}

      



There are other overloaded methods listed in the API in the MemberMatcher class that are useful in other cases, such as the method has parameters, there are additional inherited methods, etc. Hope it helps.

0


source







All Articles