英文:
Mockito on continuous Optional classes
问题
这是我正在使用的代码块:
Optional<Response> response = something.get();
Optional<String> content = response.map(Response::getContent);
ObjectNode head = objectMapper.readValue(content.get(), ObjectNode.class);
// 接下来我将使用这个 "head" 进行操作
我之前遇到过 "Cannot mock/spy class java.util.Optional" 的问题,所以我知道无法对 Optional 进行模拟。由于我没有 "something",我正在尝试以下操作:
@Mock private Something something;
@Mock Response res;
when(something.get()).thenReturn(Optional.of(res));
但我不知道如何模拟接下来的两行代码。
我已经构造了一个 "head",我想用我创建的这个 "head" 进行测试。
希望我表述清楚了。谢谢!
英文:
This is the code block I'm working with:
Optional<Response> response = something.get();
Optional<String> content = response.map(Response::getContent);
ObjectNode head = objectMapper.readValue(content.get(), ObjectNode.class);
// then I'm going to work with this "head"
I got Cannot mock/spy class java.util.Optional
before, so I know I cannot mock Optional. Since I don't have "something", I'm doing:
@Mock private Something something;
@Mock Response res;
when(something.get()).thenReturn(Optional.of(res));
But I don't know how to mock the next two lines.
I have constructed a "head" and I want to test with this one I created.
Hope I make myself clear. Thanks!
专注分享java语言的经验与见解,让所有开发者获益!
评论