Test fails after enabling feature for mocking final classes and methods
See original GitHub issueI am mocking GoogleSignInAccount with the latest Mockito version:
GoogleSignInAccount mockGoogleAccount = mock(GoogleSignInAccount.class);
when(mockGoogleAccount.getId()).thenReturn("id");
when(mockGoogleAccount.getEmail()).thenReturn("email@email.com");
when(mockGoogleAccount.getFamilyName()).thenReturn("family name");
when(mockGoogleAccount.getGivenName()).thenReturn("given name");
After enabling mocking of final classes/methods test fails with this error (which is not really helpful):
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at com.app.profile.data.ProfileRepositoryTest.shouldFailRegisterIfEmailNotRegisteredAndUnknownGenderGoogleAccount(ProfileRepositoryTest.java:870)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
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.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Let me know if you need more details from me.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:17 (12 by maintainers)
Top Results From Across the Web
How to mock a final class with mockito - java - Stack Overflow
Before Mockito can be used for mocking final classes and methods, it needs to be > configured. We need to add a text...
Read more >Mock Final Classes and Methods with Mockito - Baeldung
Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.
Read more >How to Mock Final Classes in Unit Tests | Object Partners
Extracting an interface, making a method not final, etc. are all valid options. However, it is not always practical, the class you need...
Read more >Mock final classes with Mockito - Igorski
This makes the test fail with the following exception: ... Turning on the Mockito mock-maker-inline feature adds a second MockMaker on the classpath, ......
Read more >How to Mock Final Classes in PHPUnit - Tomas Votruba
Then use the interface instead of the class in your test: <?php use PHPUnit\Framework\TestCase; final class FinalClassTest extends TestCase { ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Add the
-noverify
option into yourapp/build.gradle
.Then, run
./gradlew clean test
.Here is my Java environment:
If you run test on Android Studio, set
-noverify
asVM options
in theRun/Debug Configurations
dialog.@ChristianSchwarz, thank you for your information. However, that PR will not solve this issue.
Generally, an Android project has two kinds of tests.
That PR seems to provide inline mocking feature on Android VM using slicer library.
On the other hand, this problem occurs while running local unit tests, so unfortunately that PR will not help this issue.