This is my testing class:
class RocketListVMTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var sut: RocketListVM
private var activeOnlyToggle = false
private val repo: Repo = mock()
@Before
fun setUp() {
sut = RocketListVM(repo)
activeOnlyToggle = false
}
@Test
fun toggleActiveOnlyWithTrueCallsRepository() {
sut.toggleActiveOnly(true)
verify(repo).getActiveOnlyLocalRockets()
}
}
With the following dependencies:
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'
I have created src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker
with mock-maker-inline
inside.
The test class fails because
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@dd6cba3
Caused by: org.mockito.exceptions.base.MockitoInitializationException:
Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
How to fix this problem? None of the SO answers helped.
MockMaker is an extension point that makes it possible to use custom dynamic proxies and avoid using the default byte-buddy/asm/objenesis implementation. For example, the android users can use a MockMaker that can work with Dalvik virtual machine and hence bring Mockito to android apps developers.
Option 2: Adding a file to enable the Mock maker inlineCreate a resources directory in your test directory if you do not have one. In resources create the directory mockito-extensions and in that directory the file org. mockito. plugins.
This works:
dependencies {
testImplementation 'org.mockito:mockito-core:3.8.0'
androidTestImplementation 'org.mockito:mockito-android:3.8.0'
}
There are two packages: mockito-core is for test, while mockito-android is for android test.
ref java.lang.IllegalStateException: Could not initialize plugin: MockMaker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With