11

I am writing JUnit test case for methods similar to sample given below:

Class SampleA{
    public static void methodA(){
        boolean isSuccessful = methodB();
        if(isSuccessful){
            SampleB.methodC();
        }
    }

    public static boolean methodB(){
        //some logic
        return true;
    }
}

Class SampleB{
    public static void methodC(){
        return;
    }
}

I wrote the following test case in my test class:

@Test
public void testMethodA_1(){
    PowerMockito.mockStatic(SampleA.class,SampleB.class);

    PowerMockito.when(SampleA.methodB()).thenReturn(true);
    PowerMockito.doNothing().when(SampleB.class,"methodC");

    PowerMockito.doCallRealMethod().when(SampleA.class,"methodA");
    SampleA.methodA();
}

Now I want to verify whether static methodC() of class Sample B is called or not. How can I achieve using PowerMockito 1.6? I have tried many things but it doesn't seems to be working out for me. Any help is appreciated.

Sam
  • 376
  • 3
  • 13
Prerak Tiwari
  • 3,436
  • 4
  • 34
  • 64

1 Answers1

39

Personally, I have to say that PowerMock, etc. is the solution to a problem that you shouldn't have if your code wasn't bad. In some cases, it is required because frameworks, etc. use static methods that lead to code that simply cannot be tested otherwise, but if it's about YOUR code, you should always prefer refactoring instead of static mocking.

Anyway, verifing that in PowerMockito shouldn't be that hard...

PowerMockito.verifyStatic( Mockito.times(1)); // Verify that the following mock method was called exactly 1 time
SampleB.methodC();

(Of course, for this to work you must add SampleB to the @PrepareForTest annotation and call mockStatic for it.)

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • Thanks, but not worked for me, any idea about **[this](https://stackoverflow.com/questions/71455593/notamockexception-exception-when-trying-to-verify-a-static-method-with-powermock)** ? –  Mar 13 '22 at 10:33
  • @harry Sounds like a bug to me. Newer versions of Mockito support static mocking (see https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#48 ), so you could try that or you could refactor the code to not require static mocking for tests (preferred). Remember: You only need static mocking because the code smells. – Florian Schaetz Mar 13 '22 at 13:23
  • Thanks a lot. Then; **1.** Which versions pair can I use? **2.** How can I refactor the code to not require static mocking for tests? Do you mean test method or service method that is under test? –  Mar 13 '22 at 13:30
  • 1) I would simply use the latest Mockito version, don't get the question. 2) Personally, I would always prefer to refactor the service method (that is under test) to not require static mocking, because in most cases, the underlying design is simply not very good. That's a valuable lesson from testing: Testability leads to better code. – Florian Schaetz Mar 13 '22 at 17:25
  • Yes you right, but regarding to refactoring, there are tons of static Util methods that I have no opportunity to change. But I agree with you, it was not a good idea. –  Mar 13 '22 at 17:28
  • @harry There is always the possibility of creating a wrapper. Assume have have static function X. Now create a wrapper class that has non-static function Y, which only does one thing, call X. If you, in your own code, now only use Y, you can easily mock it. – Florian Schaetz Mar 14 '22 at 06:28
  • I look at Wrapper Class, but cannot found a proper definition except from some unuseful explanations :( Any example by code pls? –  Mar 14 '22 at 07:51