12

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.

Sorry
  • 532
  • 2
  • 6
  • 19
  • 1
    Does this answer your question? [java.lang.IllegalStateException: Could not initialize plugin: MockMaker](https://stackoverflow.com/questions/41850779/java-lang-illegalstateexception-could-not-initialize-plugin-mockmaker) – RedDeath Aug 09 '21 at 14:22
  • this fixed my issue https://stackoverflow.com/a/70844598/3675925 – Hamid Zandi Mar 01 '22 at 01:19

3 Answers3

20

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

Yin Shen
  • 256
  • 1
  • 4
  • Weird, I was only doing local unit test, but I also need to add AndroidTestImplementation import. And I need to use mockito version 3.8.0, 2.something doesn't work – BabyishTank Aug 29 '21 at 01:41
0

I faced the same issue and I have solved it by using testImplementation instead of androidTestImplementation in build.gradle (app-level):

testImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
Antoine
  • 1,393
  • 4
  • 20
  • 26
0

I got the same problem recently, added the following dependencies explicitly alongside Mockito core and it worked

    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy</artifactId>
        <version>1.12.18</version>
    </dependency>
    <dependency>
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>3.3</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy-agent</artifactId>
        <version>1.12.18</version>
        <scope>test</scope>
    </dependency>
ServletException
  • 171
  • 1
  • 15