PowerMockito如何捕获传递给模拟对象的参数



我正在尝试捕获用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. */

当我执行测试时,我有一个例外执行over overify.verify(mock).put(...);

"想要但没有调用模拟。

怎么了?

您应该在Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());

之前致电instance.methodToTest();

verify() 验证指定的方法调用 did 发生。

您无法验证应该发生方法调用的"预先"。

因此:verify()需要在事实之后发生,而不是之前!

相关内容

  • 没有找到相关文章

最新更新