Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you make mockito (1.10.17) work with default methods in interfaces?

Tags:

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)?

like image 781
fge Avatar asked Dec 27 '14 00:12

fge


People also ask

Can we mock default method?

With the advancement of Java 8, we can define default methods within the interface now. Mockito lets us mock Interface default methods as well.

Can Mockito mock an interface?

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.


1 Answers

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

like image 180
Brice Avatar answered Sep 30 '22 07:09

Brice