如何捕获(出于声明目的)传递给静态存根方法调用的参数?
methodBeingStubbed看起来像这样…
public class SomeStaticClass{
protected static String methodBeingStubbed(Properties props){
...
我存根方法调用,因为它需要验证它是否被调用…
PowerMockito.stub(PowerMockito.method(SomeStaticClass.class, "methodBeingStubbed")).toReturn(null);
PowerMockito.verifyStatic();
但是我现在也想知道什么属性传递给了这个“ methodBeingStubbed”,并断言它是预期的
解决方法:
调用verifyStatic之后,您实际上需要调用要验证的方法,如the documentation here:
PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());
此时,您可以使用Mockito argument captors,如所示(但未经测试):
ArgumentCaptor<Properties> propertiesCaptor =
ArgumentCaptor.forClass(Properties.class);
PowerMockito.verifyStatic(SomeStaticClass.class);
SomeStaticClass.methodBeingStubbed(propertiesCaptor.capture());
Properties passedInValue = propertiesCaptor.getValue();
如果您习惯使用@Mock批注,或者需要捕获通用名称(如List< String>中所示),则可能还对使用@Captor
批注感兴趣.