3

A class has a method inside which there are several calls of other class's static void methods.

i.e.

public class Foo()
{
 public void Bar()
{
  //some code
  OtherClass.callVoidMethod1();
 // some code
OtherClass.callVoidMethod2();
}
}

In my unit tests I need OtherClass.callVoidMethod2(); not to be called and was suggested to mock it. But so far I can't find how to mock static void method call. Is it even possible? Available in the project: mockito, powermock, testng.

Iwavenice
  • 429
  • 5
  • 18
  • 6
    Yes, it can. The answer can be found here: http://stackoverflow.com/questions/9585323/how-do-i-mock-a-static-method-that-returns-void-with-powermock . Of course, if you control the code, refactoring is better than fighting to symptoms of bad code with PowerMock ;-) – Florian Schaetz Dec 24 '15 at 11:20
  • Ugh I thought there would be a way to do the when(callVoidMethod2()).then(doSomething) – Iwavenice Dec 24 '15 at 12:06
  • No, the PowerMock syntax is something you need to get used to... Another reason why I prefer not to use it, instead opting to refactor code or introducing non-static objects that can be mocked (the real object calls the static method, so you can prevent that via a mock). – Florian Schaetz Dec 24 '15 at 12:28
  • Unfortunately I am not supposed to refactor the class I'm testing but I did refactor my test so that I don't need Powermock there anymore. Whew such a relief ) – Iwavenice Dec 25 '15 at 13:26

0 Answers0