Verify Static Method Call using PowerMockito 1.6

前端 未结 1 1756
北荒
北荒 2020-12-14 22:46

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

Class SampleA{
    public static void methodA(){
        boolean isSuccessful = metho         


        
相关标签:
1条回答
  • 2020-12-14 23:13

    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.)

    0 讨论(0)
提交回复
热议问题