gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:51:51 32 4
gpt4 key购买 nike

我正在尝试使用 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 argument);”

怎么了?

最佳答案

您应该在 Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture()); 之前调用 instance.methodToTest();;

关于java - PowerMockito 如何捕获传递给模拟对象的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46422952/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com