[英]PowerMockito how to capture a parameter passed to a mock object?
我正在尝试使用PowerMockito捕获输入中传递给模拟对象的参数,这是代码:
//I create a mock object
ClassMocked mock = PowerMockito.mock(ClassMocked.class);
//Create the captor that will capture the String passed in input to the mock object
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
//When the method put is executed on my mock I want the second parameter (that is a String) to be captured
Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());
//Creates the instance of the class that I want to test passing the mock as parameter
ClassToTest instance = new ClassToTest(mock);
//Executes the method that I want to test
instance.methodToTest();
/* I know that during the execution of "methodToTest()" mock.put(String,String) will be executed and I want to get the second string parameter. */
当我执行测试时,我有一个异常执行行Mockito.verify(mock).put(...);
“想要但未调用mock.put(any,Capturing参数);”
怎么了?
您应该调用instance.methodToTest();
在Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());
verify()
验证指定的方法调用确实发生了 。
您无法“预先”确定应该进行方法调用。
因此: verify()
需要在事实之后而不是之前发生!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.