如何使用Mockito捕获发送到存根方法的方法参数

时间:2016-12-12 22:02:45

标签: java unit-testing mocking mockito

这是我的简短方法:

 Mockito.when(restTemplateMock.postForEntity(anyString(), any(), eq(String.class))).thenReturn(new ResponseEntity<String>(HttpStatus.OK));

如何抓住发送到此方法的参数?上面的postForEntity方法有3个参数。

1 个答案:

答案 0 :(得分:3)

final ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Object> arg2Captor = ArgumentCaptor.forClass(Object.class);
final ArgumentCaptor<Class<?>> arg3Captor = ArgumentCaptor.forClass(Class.class);

Mockito.verify(restTemplateMock).postForEntity(arg1Captor, arg2Captor, arg3Captor);

Assert.assertEquals("Whatever", arg1Captor.getValue());