我正在编写一个单元测试,该测试访问App类中LinkedHashMap的公共变量。我想嘲笑这个返回空列表,请问我该怎么做
应用程序具有此变量
public LinkedHashMap<String, ArrayList<QCCheck>> mapOfQCC =
new LinkedHashMap<>();
单元测试要求mapOfQCC返回空列表
我尝试了这个,但没有工作
every(app.mapOfQCC).thenReturn(LinkedHashMap<String, ArrayList<QCCheck>>())
错误
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
提前感谢R
错误消息不言自明。您正在尝试存根字段访问:
every(app.mapOfQCC).thenReturn(LinkedHashMap<String, ArrayList<QCCheck>>())
Mockito不可能做到这一点。您只能存根方法调用。
您有两个选项:
- 为您的字段提供getter(并可能使字段私有(。截住吸气剂
- 在测试中设置字段。它是公开的。没有什么能阻止你那样做
LinkedHashMap<String, ArrayList<QCCheck>> keepOldIfNeed = app.mapOfQCC; // keep the list in object if you need
app.mapOfQCC = new LinkedHashMap<>(); // this is empty