question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mockito-inline can't mock finals

See original GitHub issue

Im trying to mock JsonObject using mockito-inline. When running from console it runs perfectly but when trying to use Android Studio i get

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.google.gson.JsonObject
Mockito cannot mock/spy because :
 - final class

I tried using both @Mock JsonObject jsonObject and Mockito.mock(JsonObject.class) but none of them work. Am I missing something?

Versions:

  • mockito-inline: 2.7.22 and 2.8.9
  • junit: 4.12
  • jdk: 1.8.0_92
  • Gradle: 3.5
  • Android Studio: 2.2.3 (Its a simple junit test but just in case)
Map<String, JsonElement> OUTER = new HashMap<>();
    Map<String, JsonElement> INNER = new HashMap<>();

    @Mock
    TypeAdapter<MockClass> adapter;
    @Mock
    TypeAdapter<JsonElement> elementTypeAdapter;
    @Mock
    JsonObject outer;
    @Mock
    JsonObject inner;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        INNER.put("test1", new JsonPrimitive("test1"));
        INNER.put("test2", new JsonPrimitive("test2"));
        when(inner.entrySet()).thenReturn(INNER.entrySet());

        OUTER.put("test3", new JsonPrimitive("test3"));
        OUTER.put("test4", new JsonPrimitive("test4"));
        OUTER.put("inner", inner);
        when(outer.entrySet()).thenReturn(OUTER.entrySet());
    }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
martofeldcommented, Jun 5, 2017

@szczepiq Found a workaround, quiting and opening AS again somehow makes this work. Still haven’t found what causes this.

0reactions
ali-nasercommented, Jun 28, 2022

I’ve faced similar problem. I’ve added ‘mockito-inline’ dependency from https://mvnrepository.com/artifact/org.mockito/mockito-inline/3.8.0 and followed this https://www.baeldung.com/mockito-final. Then it works. Sample code:

   private JSONMap atonQueriesMap;

   @Before
   public void setup() {
   	atonQueriesMap = mock(JSONMap.class);
        when(atonQueriesMap.getString(any())).thenReturn(testQuery);
   }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock a final class with mockito - java - Stack Overflow
Mocking final /static classes/methods is possible with Mockito v2 only. add this in your gradle file: testImplementation 'org.mockito:mockito-inline:2.13.0'.
Read more >
Mock Final Classes and Methods with Mockito - Baeldung
In this short tutorial, we'll focus on how to mock final classes and methods using Mockito. As with other articles focused on the...
Read more >
Mock final classes with Mockito - Igorski
PinProvider Mockito cannot mock/spy because : - final class ... Turning on the Mockito mock-maker-inline feature adds a second MockMaker on the classpath, ......
Read more >
Mock final classes and methods with Mockito - David Vlijmincx
Mockito doesn't support this behavior by default. To stub final methods and mock final classes, we need to enable the inline mock maker...
Read more >
Mockito Inline Fails with "Java.lang.ExceptionInitializerError"
Cannot mock /spy class [multiple classes cannot be mocked]. Mockito cannot mock/spy following: - final classes. - anonymous classes. - primitive types.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found