java - org.mockito.exceptions.misusing.WrongTypeOfReturnValue
问题描述
我正在尝试执行单元测试,但遇到 Iterable 异常
实现代码
private ClientWrapper clientWrapper;
Iterable<Document> documentIterable=clientWrapper.doOperation(reqList);
测试代码
@Mock
ClientWrapperImpl mocClientWrapper;
Document mockDocuent=Mockito.mock(Document.class);
Iterable<Document> documentIterable=Mockito.mock(Iterable.class);
Iterator<Document> documentIterator=Mockito.mock(Iterator.class);
Mockito.when(documentIterable.iterator)).thenReturn(documentIterator);
Mockito.when(documentIterator.hasNext()).thenReturn(true).thenReturn(false);
Mockito.when(documentIterator.next()).thenReturn(mockDocuent);
Mockito.when(mocClientWrapper.doOperation(Mockito.anyList())).thenReturn(documentIterable);
我得到的例外:Full StackTrace
org.mockito.exceptions.misusing.WrongTypeOfReturnValue
Iterable$MockitoMock$598 cannot be returned by toString()
toString() should return string
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1.This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2.A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies-- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
这个异常显示在我正在模拟 doOperation 方法的测试类中。
解决方案
推荐阅读
- python - 将 django 数据库条目传递到 line messenger api (linebot) 中的消息中
- r - dplyr如何计算多列的百分比并使用自定义名称输出列
- django - Django+Vue+webpack 配置
- metalava - metalava 如何将注释合并到其 API 快照中?
- amazon-web-services - 在亚马逊连接中实施 ESL
- javascript - js在数组中搜索50k结果太慢了
- javascript - 更改应用程序的行为 如何根据作为由总和和货币符号组成的字符串的一部分的数字来隐藏结帐按钮?
- transactions - 尝试从我的 Tomo 钱包发送 TOMO 时,如何解决“nonce too low”响应?
- google-apps-script - TypeError:sheet.getDataRange 不是函数
- javascript - 反转选择内的颜色