ClassCastException with mockito-inline

118 Views Asked by At

I am using Mockito version 5.2.0.

With mockito-inline, the following code throws a ClassCastException:

public static void main(String[] args) {
  A a = mock(A.class, RETURNS_DEEP_STUBS);
  String str = a.b().map(B::string).orElseThrow();
}

interface A {
  Optional<B> b();
}

interface B {
  String string();
}

Is this a bug?

The reason it throws a ClassCastException is that the expression a.b().map(B::string).orElseThrow() returns a B, not a String. To me, it would be more logical for a.b().map(B::string).orElseThrow() to return null.

0

There are 0 best solutions below