loading...

[Android] How to mock final class with Mockito2

twitter logo github logo ・1 min read

In Android project, data class is final. So final class can't be extended.

Issue

This error will be happen when data class is mocked by using mock(), spy(), etc...

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class net.kikuchy.example.ComplexData
Mockito cannot mock/spy because :
 - final class

Solution

Adding mockito-inline

dependencies {
    testImplementation "org.mockito:mockito-core:$mockito_version"
    testImplementation "org.mockito:mockito-inline:$mockito_version"
}
twitter logo DISCUSS
Classic DEV Post from Nov 13 '19

Apples announces new 16-inch Macbook Pro

Discussion thread for the new Macbook Pro thread

Rui Kowase profile image
Software Engineer

👋 Hey dev.to reader.

Do you prefer sans serif over serif?

You can change your font preferences in the "misc" section of your settings. ❤️