2017-09-26 255 views
0

我试图捕获在输入传递到与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. */ 

当我执行测试我有执行该线Mockito.verify异常(模拟) 。放(...);

“通缉但不引用mock.put(任何,捕捉参数);”

出了什么问题?

回答

2

你应该叫

+0

是的,你是对的,我把验证后,它的工作原理,但我想念它是如何工作的。 如果我把测试执行后的方法验证模拟知道它必须捕获参数? – user2572526

+0

我不确定PowerMockito的内部,但它应该记录所有调用模拟方法的参数 – chomnoue

1

verify()验证指定的方法调用走位instance.methodToTest();Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());之前。

您无法验证方法调用应该发生的“前期”。

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

+0

您的回答是正确的我接受了用户chomnoue之一,因为他先回答了。 – user2572526

相关问题