mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

Does not work with powermock: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) #382

Open rumax opened 4 years ago

rumax commented 4 years ago

Adding

testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'

to the project causes exception:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

Project uses

testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-module-junit4:1.6.6'
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
danielgomezrico commented 4 years ago

@rumax Are you using Android Studio 4?

I think that the issue is related to that. I dont have powermock and get the same error when I call the mock function:

Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
    at com.sun.proxy.$Proxy2.isTypeMockable(Unknown Source)
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:241)
    at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:229)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
    at org.mockito.Mockito.mock(Mockito.java:1908)
....

Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@66c94af4
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:22)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:19)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    ... 30 more
Caused by: java.lang.NullPointerException
    at org.mockito.internal.configuration.plugins.Plugins.getMockitoLogger(Plugins.java:66)
    at org.mockito.android.internal.creation.AndroidByteBuddyMockMaker.<init>(AndroidByteBuddyMockMaker.java:24)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49)
    ... 35 more
rumax commented 4 years ago

@danielgomezrico I have android studio 3.6.3. But it looks for me like some dependencies conflict.

Y2Kot commented 4 years ago

@danielgomezrico Are you using Android Studio 4?

I think that the issue is related to that. I dont have powermock and get the same error when I call the mock function:

Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
  at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
  at com.sun.proxy.$Proxy2.isTypeMockable(Unknown Source)
  at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
  at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
  at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:241)
  at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:229)
  at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
  at org.mockito.Mockito.mock(Mockito.java:1908)
....

Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@66c94af4
  at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
  at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
  at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
  at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:22)
  at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:19)
  at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
  ... 30 more
Caused by: java.lang.NullPointerException
  at org.mockito.internal.configuration.plugins.Plugins.getMockitoLogger(Plugins.java:66)
  at org.mockito.android.internal.creation.AndroidByteBuddyMockMaker.<init>(AndroidByteBuddyMockMaker.java:24)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at java.lang.Class.newInstance(Class.java:442)
  at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49)
  ... 35 more

I got this error on AS4. Can you help me to fix this? on AS3.6 all was ok

dsuresh-ap commented 4 years ago

This can be solved by excluding mockito-core. Ex:

testImplementation(TestLibs.mockitoKotlin) {
    exclude("org.mockito", "mockito-core")
}