This is an old question, but here is how I did it using the Mockito spy, creating a method that calls the parent method in the child class:
public class Child extends Parent { @Override protected String print() {
And in the test:
@Test public void sould_mock_invocation_of_protected_method_of_parent_class() throws Exception { // Given Child child = Mockito.spy(new Child()); Mockito.doReturn(null) .when(child) .callParent(); // When String retrieved = child.print(); // Then Mockito.verify(child, times(1)).callParent(); // verification of child method Assert.assertEquals(retrieved, "abc"); }
Note: this test only checks that we call the parent method in the child class.
source share