简体   繁体   中英

stream() method is not working with Mockito spy

Code below:

 List<Object> list = spy(new LinkedList<>());
 list.stream().map(item -> item);

is not working, causing an exception during test:

Cannot call real method on java interface. Interface does not have any implementation!
Calling real methods is only possible when mocking concrete classes.
  //correct example:
  when(mockOfConcreteClass.doStuff()).thenCallRealMethod();

However, calling size() is working okay. What is wrong with stream() method? I am using Mockito 1.8.4 ver.

The method java.util.List.stream() is a default method in java.util.Collection . Mockito versions older than 1.10.5 (maybe 1.10.0) cannot handle default methods (it's missing the java.lang.reflect.Method.isDefault() check and maybe some spy-specifics for dealing with such methods).

I've verified that it works with Mockito 1.10.19, so I recommend upgrading when writing code in Java 8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM