Mockito cannot mock final classes in Kotlin


org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class com.example.data.model.WeatherData
Mockito cannot mock/spy because :
– final class

This happens because Kotlin classes and functions are declared as final/closed by default, but Mockito cannot mock/spy the object if it is a final class.

To resolve the above issue, you need to enable the option to mock the final classes in mockito. Which is to create a file and name it org.mockito.plugins.MockMaker in app/src/test/resources/mockito-extensions/ with this line

mock-maker-inline

Reference:
https://antonioleiva.com/mockito-2-kotlin/

Search within Codexpedia

Custom Search

Search the entire web

Custom Search