I am trying to make fun of the protected method of the parent class. For this reason, I use Mockitoand PowerMockito. My parent class.
public class Parent {
protected int foo() {
throw new RuntimeException("Do not invoke this method.");
}
}
My child class.
public class Child extends Parent {
protected int boo() {
return super.foo();
}
}
And a test class.
@RunWith(PowerMockRunner.class)
@PrepareForTest({Parent.class, Child.class})
public class ChildTest {
@Mock
private Child childMock;
@Before
public void before() {
initMocks(childMock);
}
@Test
public void shouldInvokeProtectedMockedMethod() throws Exception {
PowerMockito.doReturn(500).when(childMock, "foo");
childMock.boo();
Mockito.verify(childMock, Mockito.times(1)).boo();
Mockito.verify(childMock, Mockito.times(1)).foo();
}
@After
public void after() {
Mockito.reset(childMock);
}
}
When I run it, I get this error
Wanted but not invoked:
child.foo();
-> at com.test.ChildTest.shouldInvokeProtectedMockedMethod(ChildTest.java:36)
However, there were other interactions with this mock:
child.boo();
-> at com.test.ChildTest.shouldInvokeProtectedMockedMethod(ChildTest.java:33)
What am I doing wrong?
source
share