mockito-inline can't mock finals
See original GitHub issueIm 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:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@szczepiq Found a workaround, quiting and opening AS again somehow makes this work. Still haven’t found what causes this.
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: