Mockito 2 mock-maker-inline not able to mock Object methods on an interface
See original GitHub issueHi there,
I recently upgraded from Mockito 1.9.5
to 2.7.22
(mockito-inline), and some tests started failed as Mockito is unable to mock Object.toString()
method on an interface.
It appears to be that the inline mock maker cannot mock Object methods on an interface, using a class (HashSet for the below example) or mockito-core instead of mockito-inline makes it work.
System:
OS: Windows 10 (version 1703)
Java: JDK 1.8u131
Mockito: org.mockito:mockito-inline:2.7.22 //org.mockito:mockito-core:2.7.22 works fine!
Junit: junit:junit:4.12
Sample example that fails:
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.Set;
public class TestInterfaceToString {
@Test
public void testInterfaceToString() {
Set set = Mockito.mock(Set.class); // Change Set.class to HashSet.class makes it work
Mockito.when(set.toString()).thenReturn("bla");
Assert.assertEquals("bla", set.toString());
}
}
Output:
"C:\Program Files\Java\jdk1.8.0_131\bin\java" -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\lib\idea_rt.jar=58071:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Users\Mina\Documents\IdeaProjects\TestingMockito2\out\production\TestingMockito2;C:\Users\Mina\.m2\repository\org\mockito\mockito-inline\2.7.22\mockito-inline-2.7.22.jar;C:\Users\Mina\.m2\repository\org\mockito\mockito-core\2.7.22\mockito-core-2.7.22.jar;C:\Users\Mina\.m2\repository\net\bytebuddy\byte-buddy\1.6.11\byte-buddy-1.6.11.jar;C:\Users\Mina\.m2\repository\net\bytebuddy\byte-buddy-agent\1.6.11\byte-buddy-agent-1.6.11.jar;C:\Users\Mina\.m2\repository\org\objenesis\objenesis\2.5\objenesis-2.5.jar;C:\Users\Mina\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Mina\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 TestInterfaceToString
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 TestInterfaceToString.setUp(TestInterfaceToString.java:20)
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:497)
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.RunBefores.evaluate(RunBefores.java:24)
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:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Process finished with exit code -1
Cheers, Mina
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Using mockito-inline in project throws "Mockito cannot mock ...
I wanted to mock final classes and methods, so I used mockito-inline following this post Mockito mock final classes and methods and setting...
Read more >Mocking Static Methods With Mockito - Baeldung
In this tutorial, we'll take a look at how we can now mock static methods using the latest version of Mockito.
Read more >Mockito (Mockito 4.9.0 API) - javadoc.io
Mockito doesn't mock final methods so the bottom line is: when you spy on real objects + you try to stub a final...
Read more >Mock Java Constructors With Mockito | Configuration ... - rieckpil
The new method that makes mocking object constructions possible is Mockito.mockConstruction() . This method takes a non-abstract Java class that ...
Read more >Mocking Private, Static and Void Methods Using Mockito
Powermock – A Brief Introduction. For Mockito, there is no direct support to mock private and static methods. In order to test private...
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
@johnjohndoe There is not a minium JDK version for Mockito, but there are a small subset of errors that are JDK related and fixed in more recent versions. I think all versions > 1.8.45 are good enough for this subset.
Yes, its a bug in the inline mock maker. Fixed in attached PR.