Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange NoSuchMethodError when using 2.2.0 #364

Closed
BoD opened this issue Oct 17, 2019 · 14 comments · Fixed by #417
Closed

Strange NoSuchMethodError when using 2.2.0 #364

BoD opened this issue Oct 17, 2019 · 14 comments · Fixed by #417

Comments

@BoD
Copy link

BoD commented Oct 17, 2019

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?

@BoD BoD changed the title Strange NoSuchMethodError when using 2.2.0 Strange NoSuchMethodError when using 2.2.0 Oct 17, 2019
@bohsen
Copy link

bohsen commented Oct 17, 2019

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
Copy link
Author

BoD commented Oct 17, 2019

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
Copy link

bohsen commented Oct 17, 2019

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
Copy link
Author

BoD commented Oct 17, 2019

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
Copy link
Author

BoD commented Oct 17, 2019

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
Copy link

bohsen commented Oct 17, 2019

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
Copy link
Author

BoD commented Oct 17, 2019

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
Copy link
Collaborator

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
Copy link
Author

BoD commented Oct 19, 2019

That's correct.

@bohsen
Copy link

bohsen commented Oct 21, 2019

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

@jleidgens
Copy link

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 to import com.nhaarman.mockitokotlin2.mock
the test works fine.

@BoD
Copy link
Author

BoD commented Feb 6, 2020

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
Copy link
Author

BoD commented Mar 5, 2021

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
Copy link
Contributor

hennr commented Mar 22, 2021

See #408 as well for updated dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants