Reputation: 4388
I am writing a unit test which access a public variable of LinkedHashMap in App class. I want to mock this to return empty list, how can I do that please
App has this variable
public LinkedHashMap<String, ArrayList<QCCheck>> mapOfQCC =
new LinkedHashMap<>();
Unit test requires mapOfQCC to return empty list
I tried this and did not work
every(app.mapOfQCC).thenReturn(LinkedHashMap<String, ArrayList<QCCheck>>())
Error
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Thanks in advance R
Upvotes: 1
Views: 568
Reputation: 26066
The error message is self explanatory. You are trying to stub field access:
every(app.mapOfQCC).thenReturn(LinkedHashMap<String, ArrayList<QCCheck>>())
This is not possible with Mockito. You can only stub method calls.
You have 2 options:
Upvotes: 1
Reputation: 466
LinkedHashMap<String, ArrayList<QCCheck>> keepOldIfNeed = app.mapOfQCC; // keep the list in object if you need
app.mapOfQCC = new LinkedHashMap<>(); // this is empty
Upvotes: 0