BRDroid
BRDroid

Reputation: 4388

Is it possible to return empty list of LinkedHashMap using Mockito in a test case

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

Answers (2)

Lesiak
Lesiak

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:

  • provide a getter for your field (and, possibly, make the field private). Stub the getter.
  • set the field in your test. It is public. Nothing prevents you from doing that.

Upvotes: 1

Frighi
Frighi

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

Related Questions