I was writing tests for a method that takes in a Consumer and I found i was able to create a spy on this functional interface.
Consumer<Integer> dfConsumer = x -> log.debug("number: {}",x);
Consumer<Integer> dfConsumerSpy = spyConsumer(dfConsumer);
However, I tried to make a spy out out of a Runnable and that did not work. I get this error that follows.
Runnable runnable = () -> log.debug("test");
Runnable runnableSpy = spy(runnable);
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class ... $$Lambda$1068/0x00000008008cf440
Mockito cannot mock/spy because :
- VM does not support modification of given type
I see that the VM doesn’t support it, but why? I’d accept if all FunctionalInterfaces had this behavior as well, but the fact that I can create a spy on a Consumer throws me off.