mockito / mockito-kotlin

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

Strange NoSuchMethodError when using 2.2.0 #364

Closed BoD closed 3 years ago

BoD commented 5 years ago

I've been trying to upgrade from 2.1.0 to 2.2.0 and now I have a few tests that fail with this exception:

java.lang.NoSuchMethodError: com.nhaarman.mockitokotlin2.MockingKt.withSettings([Lkotlin/reflect/KClass;Ljava/lang/String;Ljava/lang/Object;Lorg/mockito/stubbing/Answer;ZLorg/mockito/mock/SerializableMode;Z[Lorg/mockito/listeners/InvocationListener;ZLcom/nhaarman/mockitokotlin2/UseConstructor;Ljava/lang/Object;Z)Lorg/mockito/MockSettings;

    at com.qonto.qonto.ui.otp.setup.phonenumber.PhoneNumberPresenterTest.<init>(PhoneNumberPresenterTest.kt:180)
    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 org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

This is on a test that does something like this:

    private val otpViewModel = mock<OTPSetupViewModel> {
        on { mustSwitchOtpFromAppToSms() } itAnswers { mustSwitchOtpFromAppToSms }
        on { createMfaBySms() } itAnswers { createMfaBySmsResponse }
        on { showError } itAnswers { showErrorLiveData }
    }

Any idea?

bohsen commented 5 years ago

private val otpViewModel = mock { on { mustSwitchOtpFromAppToSms() } itAnswers { mustSwitchOtpFromAppToSms } on { createMfaBySms() } itAnswers { createMfaBySmsResponse } on { showError } itAnswers { showErrorLiveData } }

@BoD There's no itAnswers method in Mockito-kotlin.

BoD commented 5 years ago

Oh you're right, this is from Kluent actually... But still this only happens when updating the version of mockito-kotlin. Do you have an idea of what could be the issue, or should I try to open an issue on the Kluent project? 😊

bohsen commented 5 years ago

I would check my imports. I don't know Kluent, but my guess is that on might be the from Mockito-kotlin, where it probably should have used on from Kluent?

BoD commented 5 years ago

So I checked and there is no on in Kluent - but if I use doAnswer from mockito-kotlin, I still have the same error.

Here's my imports:

import com.nhaarman.mockitokotlin2.doAnswer
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.timeout
import com.nhaarman.mockitokotlin2.verify
(other imports omitted but nothing from Kluent anymore)

and the modified version:

    private val otpViewModel = mock<OTPSetupViewModel> {
        on { mustSwitchOtpFromAppToSms() } doAnswer { mustSwitchOtpFromAppToSms }
        on { createMfaBySms() } doAnswer { createMfaBySmsResponse }
        on { showError } doAnswer { showErrorLiveData }
    }

Any idea what could be wrong?

BoD commented 5 years ago

Oh I have more info: I also upgraded org.mockito:mockito-core from 3.0.0 to 3.1.0. If I revert to 3.0.0 I no longer have this issue. That makes sense?

bohsen commented 5 years ago

Oh I have more info: I also upgraded org.mockito:mockito-core from 3.0.0 to 3.1.0. If I revert to 3.0.0 I no longer have this issue. That makes sense?

Makes sense, as Mockito-kotlin internally relies on Mockito. There's probably a breaking change in Mockito.

BoD commented 5 years ago

All right thank you. I just looked and I notice Mockito-kotlin depends on a fairly old version of org.mockito:mockito-core (2.23.0). I guess it would be cool to upgrade that. In the meantime I think on my side I'll stay with mockito-kotlin 2.1.0.

Thanks!

nhaarman commented 5 years ago

So if I understand correctly mockito-kotlin 2.2.0 works fine with Mockito 3.0.0 but not with Mockito 3.1.0, correct?

BoD commented 5 years ago

That's correct.

bohsen commented 5 years ago

@BoD Could you post a bit more of the test code? Can't reproduce the issue with upgraded mockito-core dependency.

jleidgens commented 5 years ago

I had the same issue and we also use Kluent. In this Test it turned out we used the mock method from Kluent instead of mockitotokotlin and with that we got:

java.lang.NoSuchMethodError: com.nhaarman.mockitokotlin2.MockingKt.withSettings([Lkotlin/reflect/KClass;Ljava/lang/String;Ljava/lang/Object;Lorg/mockito/stubbing/Answer;ZLorg/mockito/mock/SerializableMode;Z[Lorg/mockito/listeners/InvocationListener;ZLcom/nhaarman/mockitokotlin2/UseConstructor;Ljava/lang/Object;)Lorg/mockito/MockSettings;

After changing from import org.amshove.kluent.mock toimport com.nhaarman.mockitokotlin2.mock the test works fine.

BoD commented 4 years ago

Sorry to reply so late. I can confirm that we do use the mock method from mockitokotlin and have the issue so that's not the culprit.

If a new version of mockito-kotlin with an up-to-date version of the dependency to mockito could be released, I can easily see if that resolves the issue. Plus I guess it's generally a good idea to be up to date ;)

BoD commented 3 years ago

It's been several years :) Would it be possible to try to make mockito-kotlin depend on a recent version of mockito?

Currently it depends on 2.23.0 from Oct 2018, while the latest is 3.8.0 from Feb 2021.

hennr commented 3 years ago

See #408 as well for updated dependencies.