public class Abcd {
public String one() {
System.out.println("Inside method one");
StringBuilder sb = new StringBuilder();
two(sb);
return "Done";
}
private void two(StringBuilder sb) {
System.out.println("Inside method two");
}
}
Here is the test class
@RunWith(PowerMockRunner.class)
@PrepareForTest(Abcd.class)
public class TestAbcd {
@Test
public void testMethod1() throws Exception {
Abcd abcd = PowerMockito.spy(new Abcd());
StringBuilder sb = new StringBuilder();
PowerMockito.doNothing().when(abcd, "two", sb);
abcd.one();
}
}
Console Output:
Inside method one
Inside method two
No failure trace in edited part: Failure trace:
Please let me know what mistake i am making, and how can i make it work.
You need @PrepareForTest annotaion to gain such a control on private methods with PowerMockito.
See this article: What does @PrepareForTest in PowerMock really mean?
In summary the test case should look like this:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Abcd.class)
public class TestAbcd {
@Test
public void testMethod1() throws Exception {
Abcd abcd = PowerMockito.spy(new Abcd());
PowerMockito.doNothing().when(abcd, method(Abcd.class, "two", StringBuilder.class))
.withArguments(any(StringBuilder.class));
abcd.one();
}
}