java 捕获参数传递到 powermockito 中的存根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25763338/
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
capture parameters passes to stub in powermockito
提问by Rob McFeely
How can I capture (for assertion purposes) the parmeters passed to a static stub method call?
如何捕获(出于断言目的)传递给静态存根方法调用的参数?
The methodBeingStubbed looks like this...
方法BeingStubbed 看起来像这样......
public class SomeStaticClass{
protected static String methodBeingStubbed(Properties props){
...
I am stubbing the method call because it i need to verify that it gets called...
我正在对方法调用进行存根,因为我需要验证它是否被调用...
PowerMockito.stub(PowerMockito.method(SomeStaticClass.class, "methodBeingStubbed")).toReturn(null);
PowerMockito.verifyStatic();
But I now also want to know what properties were passed to this "methodBeingStubbed" and assert it is as expected
但我现在也想知道传递给这个“methodBeingStubbed”的属性并断言它是预期的
回答by Jeff Bowman
After the call to verifyStatic
, you'll need to actually call the method you're trying to verify, as in the documentation here:
在调用 之后verifyStatic
,您需要实际调用您尝试验证的方法,如此处的文档所示:
PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());
At that point you can use Mockito argument captors, as demonstrated (but not tested):
此时您可以使用 Mockito参数captors ,如演示(但未测试):
ArgumentCaptor<Properties> propertiesCaptor =
ArgumentCaptor.forClass(Properties.class);
PowerMockito.verifyStatic(SomeStaticClass.class);
SomeStaticClass.methodBeingStubbed(propertiesCaptor.capture());
Properties passedInValue = propertiesCaptor.getValue();
If you're used to @Mock
annotations, or you need to capture a generic (as in List<String>
), you may also be interested in using the @Captor
annotation instead.
如果您习惯使用@Mock
注释,或者需要捕获泛型(如 中所示List<String>
),您可能也对使用@Captor
注释感兴趣。