I am a big fan of mockito, unfortunately for one of my projects which uses Java 8, it fails on me...
Scenario:
public final class MockTest { @Test public void testDefaultMethodsWithMocks() { final Foo foo = mock(Foo.class); //when(foo.bar()).thenCallRealMethod(); assertThat(foo.bar()).isEqualTo(42); } @FunctionalInterface private interface Foo { int foo(); default int bar() { return 42; } } }
Unfortunately, the test fails and foo.bar()
returns 0.
When I uncomment the when()
line, I get a stack trace...
java.lang.NoSuchMethodError: java.lang.Object.bar()I at com.github.fge.lambdas.MockTest.testDefaultMethodsWithMocks(MockTest.java:18)
This is the latest stable version available on maven; googling around didn't tell me much about the status of mockito with regards to this new functionality in Java 8...
Can you make it work in some other way than implementing interfaces and spy()
on them (this works)?
With the advancement of Java 8, we can define default methods within the interface now. Mockito lets us mock Interface default methods as well.
The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.
With mockito 2.x, JDK 8 default methods are supported.
With mockito 1.x it's not possible,
Old answer
Unfortunately it's not yet possible (mockito 1.10.19), from the README.md
on the github'page
JDK8 status
Mockito should work fine with JDK8 if you stay away from default methods (aka defender methods). Lambda usage may work just as good for Answers. We're unsure about every JDK8 features at the moment, like serializing a mock that uses a lambda. Error report and pull request are welcome though (contributing guide).
EDIT 1: defender methods and default methods are different names for the same thing.
I hope for a mockmaker replacement that will handle java 8 opcodes properly for such cases as some opcodes have a different semantic in Java 8.
EDIT 2: Updated the mockito readme, and this quote accordingly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With