New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocktio 5.0.x and later on Java 11/17 fails when mocking interface default methods #3013
Comments
Same problem on OpenJDK 17: $ java -version
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)
jblumWLVDQ:cp-labs jblum$
Again, I don't think Java version matters from |
It doesn't even matter if the interface Cache<KEY extends Comparable<KEY>, VALUE> extends Iterable<KEY> {
boolean contains(KEY key);
@SuppressWarnings("unchecked")
default boolean containsAll(KEY... keys) {
return Arrays.stream(keys).allMatch(this::contains);
}
} |
Given a custom interface ExtList<T> extends List<T> {
@SuppressWarnings("unchecked")
default boolean containsAll(T... elements) {
return Arrays.stream(elements).allMatch(this::contains);
}
} This test case passes: @Test
@SuppressWarnings("unchecked")
public void listContainsAllElementsReturnsTrue() {
ExtList<Object> mockList = mock(ExtList.class);
doReturn(true).when(mockList).contains(any());
doCallRealMethod().when(mockList).containsAll(any(Object[].class));
assertThat(mockList.containsAll("ONE", "TWO")).isTrue();
verify(mockList, times(1)).containsAll(eq("ONE"), eq("TWO"));
Arrays.asList("ONE", "TWO").forEach(element ->
verify(mockList, times(1)).contains(eq(element)));
verifyNoMoreInteractions(mockList);
} Though, I am not sure why the Update Gist: https://gist.github.com/jxblum/570a58ccf0e4590c3a97b82901c95952 |
After a bit more experimentation, the test only passes when the arrangement on the mock is the following: doCallRealMethod().when(mockCache).containsAll(any(), any()); Given 2 arguments. How to mock a vararg method in Mockito |
Stubbing varargs methods has changed in Mockito 5 and is documented in the release notes: https://github.com/mockito/mockito/releases/tag/v5.0.0 |
According to the Javadoc for doCallRealMethod().when(mockCache).containsAll(any()); |
The JavaDoc has been updated in #3003 |
I am running Java
17
with JUnit5.9.3
and Mockito5.3.1
on Mac OS13.3.1
.Java Version
$ java -version java version "17.0.7" 2023-04-18 LTS Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224) Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)
Maven Dependencies
The following example:
https://gist.github.com/jxblum/570a58ccf0e4590c3a97b82901c95952
Does not work correct with Mockito
5.0.0
or later, up to and including5.3.1
.This used to work on Mockito
4.11.0
(pre5.0
) but no longer works!The following error occurs:
This is even a problem on Java 11, though I suspect the Java version has little to do with the underlying issue.
The text was updated successfully, but these errors were encountered: