0

My java class has 2 private methods, method1 and method2. method1 invokes method2. How can I test method1 at the same time mocking method2 using any of the java testing frameworks?

Edited question: I am using this syntax to invoke a private method: Whitebox.invokeMethod(myClass, "myPrivateMethod","myParameter");

"myPrivateMethod" invokes another private method: "anotherPrivateMethod". I would like to mock "anotherPrivateMethod". I usually do this using syntax: PowerMockito.doReturn("mockedResponse").when(myClass, "anotherPrivateMethod", "anyParameter");

The combination above does not seem to work. Basically, when I first use PowerMockito to mock "anotherPrivateMethod" and use Whitebox to invoke the "myPrivateMethod", the mock is not working. I am not getting "mockedResponse" but instead the actual "anotherPrivateMethod" is getting invoked. What am I doing wrong?

Kriss
  • 56
  • 4
  • @Tunaki: There is a lot of debate on why one should or should directly test private methods in that link. My question is straight forward. What is the syntax for testing a private method while mocking another private method it invokes using the existing java testing frameworks (Powermock, JUnit, etc) and also not having to directly use reflection (although those frameworks may use reflection). – Kriss Jan 01 '16 at 19:14
  • edited my question just now. – Kriss Jan 01 '16 at 19:38

0 Answers0