DEV Community

Rui Kowase
Rui Kowase

Posted on

[Android] How to mock final class with Mockito2

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"
}

Top comments (0)