Skip to content

Spying on interfaces with default methods does not work anymore with JDK 17 (Temurin) #2587

@Kabhal

Description

@Kabhal

Hello,

It seems that spying on interfaces to use real implementations of defender (default) interface methods is not working anymore with Java 17.

Java 17 (Temurin) :

openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8)
OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode, sharing)

Tested Mockito versions : 3.3.3 and 4.3.1

Reproducing sample (working with JDK 11) :

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.spy;

import org.junit.jupiter.api.Test;

public class MockitoJava17Issue {

   @Test
   public void testMockitoIssue() {
      MyInterface myInterface = spy(MyInterface.class);

      assertEquals("default", myInterface.name());
   }
}

interface MyInterface {

   default String name() {
      return "default";
   }
}

->

org.mockito.exceptions.base.MockitoException: 
Cannot call abstract real method on java object!
Calling real methods is only possible when mocking non abstract method.
  //correct example:
  when(mockOfConcreteClass.nonAbstractMethod()).thenCallRealMethod();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions