Search code examples
androidunit-testingandroid-studiokotlinandroid-9.0-pie

java.lang.IllegalArgumentException: On Android P, a class injection can only be applied to BaseDexClassLoader


Our application is in development process where we are following MVP approach to provide better test coverage by writing unit test cases on our presenter.

Prior to Android P, our test cases were executing without any problem but now after Android P release when we are trying to run test cases on Pixel 2 (Android P), our test cases are failing on following line of code-

private lateinit var mPackageInfo: PackageInfo
mPackageInfo = Mockito.mock(PackageInfo::class.java)

with following error message -

org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class android.content.pm.PackageInfo.

Mockito can only mock non-private & non-final classes.

Underlying exception : java.lang.IllegalArgumentException: On Android P, a class injection can only be applied to BaseDexClassLoader: net.bytebuddy.dynamic.loading.MultipleParentClassLoader@a3838ec


How can i resolve this issue?


Solution

  • By refering to here https://github.com/mockito/mockito/issues/1376

    Try to use the version 2.18.3

    testImplementation 'org.mockito:mockito-core:2.18.3'
    androidTestImplementation 'org.mockito:mockito-android:2.18.3'