java - How does a Mockito spy know when it is spying? -



java - How does a Mockito spy know when it is spying? -

this code documentation totally baffling me:

list list = new linkedlist(); list spy = spy(list); when(spy.size()).thenreturn(100); // <--- how spy know // not phone call real method???? //using spy calls *real* methods spy.add("one"); spy.add("two");

i it, mockito weird , hardly still in java. confusing thing spy.* has evaluate before knows whether it's wrapped in when() or something. how on earth first spy.* method not phone call on real object later ones doe?

according documentation first when(spy.size()).thenreturn(100) invoke real list.size() method, see: http://mockito.github.io/mockito/docs/current/org/mockito/mockito.html#13

each subsequent phone call of course of study homecoming mocked result.

if don't want real method called (e.g. when(spy.get(0)).thenreturn(...) throw indexoutofboundsexception, have utilize pattern: doreturn(...).when(spy).get(0);

java unit-testing mocking mockito spy

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -