PowerMockito.verifyStatic() Problems

与世无争的帅哥 提交于 2019-12-05 22:01:35

问题


I need to use PowerMockito to test if a specific static method is called. I am using the following PowerMockito and JUnit libraries ...

  • powermock-mockito-1.6.2-full.jar
  • junit-4.12.jar

I am having issues getting the PowerMockito.verifyStatic() method to work properly. In the following code example, I have tried using the @PrepareForTest, and mockStatic(), and I have tried excluding them. In the code example I include them.

Test Class:

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class PowerMockTest {
    @Test
    public void staticVerifyTest() {
        PowerMockito.mockStatic(Test1.class);

        // Test
        PowerMockito.verifyStatic();
        //Test1.staticMethod();
    }
}

Class Under Test:

public class Test1 {
    public static void staticMethod() {
        System.out.println("Static Method!");
    }
}

The test passes when it is run, but it should fail because Test1.staticMethod() is never called. Any help on this would be greatly appreciated!


回答1:


Alright, I figured it out thanks to Stefan Birkner's reference

Here is the correction to my sample code:

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Test1.class)
public class PowerMockTest {
    @Test
    public void staticVerifyTest() {
        PowerMockito.mockStatic(Test1.class);
        // Test
        Test1.staticMethod();
        PowerMockito.verifyStatic();
        Test1.staticMethod();
    }
}

After the static method is invoked, you need to verify that it was called by calling it again after your verifyStatic() call.

i.e.

        Test1.staticMethod();
        PowerMockito.verifyStatic();
        Test1.staticMethod();

You can also check if it was called multiple times like this...

Test1.staticMethod();
Test1.staticMethod();
Test1.staticMethod();
PowerMockito.verifyStatic(Mockito.times(3));
Test1.staticMethod();



回答2:


It's not an answer in itself but A) a confirmation that Oliver's comment about the method change is still valid at PowerMock 2.0.2 and B) a note with additional information on how it works.

PowerMockito.verifyStatic() calls Mockito.verify() which has a few examples at its Javadoc:

verify(mock, times(5)).someMethod("was called five times");
verify(mock, atLeast(2)).someMethod("was called at least two times");

As this sintax is not available anymore we need 2 lines of code to declare the validation rule. Using John's example this means that the first 3 lines would be "actual" business calls and the one after verifyStatic is just telling it which call counter must match the 2nd parameter:

PowerMockito.mockStatic(Test1.class);

// Test
Test1.staticMethod();
Test1.staticMethod();
Test1.staticMethod();

// Validation
PowerMockito.verifyStatic(Test1.class, Mockito.times(3));
Test1.staticMethod();



回答3:


For me, I encountered this issue when upgrading to powermock 2.0.0-beta.5 from 1.7.0 and so these solutions posted above DID NOT help solve my problem. Instead, I had to add the mocked class inside of the verify static call as posted within the documentation for powermock (https://static.javadoc.io/org.powermock/powermock-api-mockito/1.7.1/deprecated-list.html)

So I went from:

PowerMockito.verifyStatic(Mockito.times(1));

to:

PowerMockito.verifyStatic(Test1.class, Mockito.times(1));

This solved my problem when using the libraries below:

testCompile group: 'org.mockito', name: 'mockito-core', version: '2.18.0'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.0-beta.5'


来源:https://stackoverflow.com/questions/32976855/powermockito-verifystatic-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!