Java PowerMockito.verifyStatic() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32976855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerMockito.verifyStatic() Problems
提问by John A Qualls
I need to use PowerMockito to test if a specific static method is called. I am using the following PowerMockito and JUnit libraries ...
我需要使用 PowerMockito 来测试是否调用了特定的静态方法。我正在使用以下 PowerMockito 和 JUnit 库...
- powermock-mockito-1.6.2-full.jar
- junit-4.12.jar
- 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.
我在使 PowerMockito.verifyStatic() 方法正常工作时遇到问题。在以下代码示例中,我尝试使用 @PrepareForTest 和 mockStatic(),并尝试排除它们。在代码示例中,我包含了它们。
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!
测试在运行时通过,但它应该会失败,因为从未调用 Test1.staticMethod()。对此的任何帮助将不胜感激!
回答by John A Qualls
Alright, I figured it out thanks to Stefan Birkner's reference
好的,多亏了 Stefan Birkner 的参考,我才知道
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.
调用静态方法后,您需要在调用 verifyStatic() 后再次调用它来验证它是否被调用。
i.e.
IE
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();
回答by Oliver Collins
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 NOThelp 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)
对于我来说,我遇到了这个问题,当升级到powermock2.0.0-beta.5
从1.7.0
等这些解决方案上面贴没有帮助解决我的问题。相反,我必须在 powermock 文档中发布的 verify static 调用中添加模拟类(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'
回答by Fabio
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.
这本身不是答案,而是 A) 确认 Oliver 关于方法更改的评论在 PowerMock 2.0.2 中仍然有效,以及 B) 附有有关其工作原理的附加信息的说明。
PowerMockito.verifyStatic()calls Mockito.verify()which has a few examples at its Javadoc:
PowerMockito.verifyStatic()调用Mockito.verify(),它的 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 verifyStaticis just telling it which call counter must match the 2nd parameter:
由于此语法不再可用,我们需要 2 行代码来声明验证规则。使用 John 的示例,这意味着前 3 行将是“实际”业务调用,而verifyStatic之后的一行只是告诉它哪个调用计数器必须与第二个参数匹配:
PowerMockito.mockStatic(Test1.class);
// Test
Test1.staticMethod();
Test1.staticMethod();
Test1.staticMethod();
// Validation
PowerMockito.verifyStatic(Test1.class, Mockito.times(3));
Test1.staticMethod();