3

I need to write a test case for a class similar to the following class:

public class TestClass {

public String loadBean(int i) {
    String s = null;
    if (i <= 0) {
        s = "less";
        // call some webservice that we don't need to test
        callWebService(s);
    } else {
        s = "more";
    }
    return s;
}

private void callWebService(String s) {
    //Some complex code here

}

I need to test loadBean method only, problem is - i'm not able to skip method call to callWebService(), using PowerMock. So to explain more clearly I need to a test case which calls loadBean but do nothing when it encounters callWebService method continuing the execution flow.

I read it on this same site that we can do it with PowerMockito but could not get it to work.

Please help.

user3367569
  • 103
  • 1
  • 1
  • 9

1 Answers1

2

This worked for me:

import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.doNothing;

//Skipping rest of the code
TestClass testSpy = PowerMockito.spy(new TestClass());
doNothing().when(testSpy, method(TestClass.class, "callIt")).withNoArguments();

The code above skips the private void callIt() method of my class.

karlihnos
  • 415
  • 1
  • 7
  • 22
user3367569
  • 103
  • 1
  • 1
  • 9