Interface Cloning Measurement Method

Failing the clone() method on the interface no longer works with Mockito 2.1.0 . Below code works fine with Mockito 1.10.19 , but throws IllegalAccessError with Mockito 2.1.0 :

 public interface CloneableInterface extends Cloneable { CloneableInterface clone(); } public class CloneableInterfaceTest { @Test public void test() { CloneableInterface i = Mockito.mock(CloneableInterface.class); Mockito.when(i.clone()).thenReturn(i); // Throws IllegalAccessError } } 

Test result:

 java.lang.IllegalAccessError: CloneableInterface$MockitoMock$833899610.clone()LCloneableInterface; 

I checked the mockito error list and searched a bit, but found nothing. I wanted to check the SO community before reporting a bug with the mockito team.

Environment: JDK 1.8.0_102 , JUnit 4.11 , Mockito 2.1.0 , Gradle 3.0 , Windows 10

EDIT: Failed mockito command here

+5
source share
1 answer

This is a bug in Mockito.

Since Object::clone is protected , and since Java supports subclass inheritance over interface inheritance, an automatically created subclass overrides clone as a protected method, not a public method. Of course, Mockito should display all overridden methods as public , but currently Mockito does not. It is, however, trivial to fix. Expect this to be allowed in a future version.

+3
source

Source: https://habr.com/ru/post/1257993/


All Articles