I try to learn PowerMockitoand write a demo
This the parent class A
public abstract class A {
protected String getRoot(Long id) {
if (id == 0) {
return "root";
}
return "not root";
}
}
And this is the childA
public class ChildA extends A {
private String info;
public String getRootInfo(Long id) {
String rootInfo = getRoot(id);
return rootInfo;
}
private void initRootInfo(String info) {
this.info = info;
}
}
And now i want to mock the method A::initRootInfo,so i write a test case like this
@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class, ChildA.class})
public class ChildATest {
@Spy
@InjectMocks
private ChildA childA = PowerMockito.spy(new ChildA());
@Test
public void getRootInfoUT() throws Exception {
PowerMockito.doNothing().when(childA, "initRootInfo", Mockito.anyString());
String rootInfo = childA.getRootInfo(0L);
Assertions.assertEquals("root", rootInfo);
}
}
When i run the test case, the PowerMockito call the real method initRootInfo, so that i get a little bit confused why the A::initRootInfo will be called really, shouldn't it be mock and replaced by PowerMockito? is there have something wrong i use PowerMockito?
How to mock private method by PowerMockito correctly