[英]Mockito ArgumentCaptor for Optional arguments
Have a method in accountHandler class with following signature 在accountHandler类中具有以下签名的方法
public void processMessage(String accountId,
Metrics metrics,
Optional<ZonedDateTime> parsedDatetime) {
// do something
}
In the test, if I want to use argument captor, how do I define it? 在测试中,如果要使用参数捕获器,该如何定义?
Tried the following and it didn't work as expected. 尝试了以下操作,但未按预期工作。
ArgumentCaptor<ZonedDateTime> timeCaptor = ArgumentCaptor.forClass(ZonedDateTime.class);
What is the syntax to make this optional with argcaptor? 用argcaptor使其可选的语法是什么?
As per the similar question , use @Captor annotation: 根据类似的问题 ,使用@Captor批注:
@Captor
private ArgumentCaptor<Optional<ZonedDateTime>> captor;
The below case is for a method signature of: 以下情况是方法签名的情况:
public void processMessage(Optional<ZonedDateTime> parsedDatetime) {
Since I don't know which Meteric
class is used, I also hope AccountHandler class exists. 由于我不知道使用哪个
Meteric
类,因此我也希望存在Meteric
类。
@Test
@SuppressWarnings("unchecked")
public void withValue() {
ArgumentCaptor<Optional<ZonedDateTime>> timeCaptor = ArgumentCaptor.forClass(Optional.class);
AccountHandler accountHandler = mock(AccountHandler.class);
Optional<ZonedDateTime> input = Optional.of(ZonedDateTime.now());
accountHandler.processMessage(input);
verify(accountHandler).processMessage(timeCaptor.capture());
Optional<ZonedDateTime> optional = timeCaptor.getValue();
assertTrue(optional.isPresent());
assertNotNull(optional.get());
}
@Test
@SuppressWarnings("unchecked")
public void nullOptional() {
ArgumentCaptor<Optional<ZonedDateTime>> timeCaptor = ArgumentCaptor.forClass(Optional.class);
AccountHandler accountHandler = mock(AccountHandler.class);
Optional<ZonedDateTime> input = null;
accountHandler.processMessage(input);
verify(accountHandler).processMessage(timeCaptor.capture());
Optional<ZonedDateTime> optional = timeCaptor.getValue();
assertNull(optional);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.