Verify Super Method Call

3,415 views
Skip to first unread message

Richard Mealing

unread,
Oct 15, 2015, 12:40:33 PM10/15/15
to PowerMock
Hi all,

Is there a way to verify that a non-accessible parent class overridden method is called in a child class overriding method? For example given

public class Parent {
protected void method(...) {
...

}
}


public class Child extends Parent {
@Override
protected void method(...) {
        ...
        super.method(...);
        ...
}
}

Can we verify that in Child's overridden method, it calls Parent's method?

I know this isn't a new question as I have searched online and found multiple threads, but none have solutions (at least when the method is non-accessible) and the threads are fairly old now.

The following doesn't work, but I would expect something like

@RunWith(PowerMockRunner.class)
@PrepareForTest({Parent.class,Child.class})
public class ChildTest {
@Test
public void testMethod() {
      Parent parent = spy(new Parent());
      Child child = spy(new Child());
      Whitebox.invokeMethod(child, "method"...);
      // Attempt 1 fails as child and parent are not linked
      Mockito.verify(parent, Mockito.times(1));
      Whitebox.invokeMethod(parent, "method"...);
      // Attempt 2 fails as it doesn't realise this is a reference to the parent's method even with the cast
      Mockito.verify(child, Mockito.times(1));
      Whitebox.invokeMethod((Parent)child, "method"...);
   }
}

I know one suggestion is to refactor and use composition instead of extending, but I would like to know if there is a way to do it without refactoring, or if there are any plans for this?

Thanks

Johan Haleby

unread,
Oct 16, 2015, 3:36:55 AM10/16/15
to powe...@googlegroups.com
I don't think this is supported, sorry.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Richard Mealing

unread,
Oct 16, 2015, 3:45:42 AM10/16/15
to PowerMock
No problem, I'm not very proficient with PowerMockito yet and was wondering if I missed something!

I really hope this can be added in the future =)
Reply all
Reply to author
Forward
0 new messages