0

I am using powermock. I am facing issues with below scenario.

UPDATE:

My Question is different. The example given in another link, has private method returning some value. Here in my case both the methods are returning Void.

class ClassForWhichTestCasesIsPrepared {

    private void myPrivateMethod(String param1, MyBean param2) {
        //Some Code Here to save data
    }

    public void myPublicMethod() {
        //Some Code Here to find the require paramters to pass to below method

        myPrivateMethod(String param1, MyBean param2);
    }

}

Facing Issues for Writing Test Cases for myPublicMethod to Mock the private method in same class.

I want to mock the myPrivateMethod method, as it should not be called but myPublicMethod should be covered for test cases. Both the methods are void.

I cannot change this design, I just have to complete and cover the required test cases for same.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
  • Possible duplicate of [How to mock private method for testing using PowerMock?](https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using-powermock) – Florian S. Jun 27 '17 at 12:07

1 Answers1

0

You can use spy method provided by PowerMock.

e.g.

ClassForWhichTestCasesIsPrepared spy = PowerMockito.spy(new ClassForWhichTestCasesIsPrepared());

  when(spy, method(ClassForWhichTestCasesIsPrepared .class, "myPrivateMethod", 
  String.class, MyBean.class)).withArguments(anyString(), anyObject())
  .thenReturn(true);// this return can be made to do nothing as well if you want

spy.myPublicMethod();
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Sakalya
  • 568
  • 5
  • 15