According to the API docks - both can be used to mock methods or fields. The difference is that with a mock, you are creating a complete mock or fake object whilst with a spy, there is the real object and you just spying or stubbing out specific methods.
When using mock objects (e.g. @Mock), the default behaviour of the method when not stub is do nothing. Meaning, if its a void method, then it will do nothing when you call the method or if its a method with a non void return type then it may return null, empty or the default value.
When working as a spy, then a whole object actually does exist, Because the object is real, the method is real if do not stub out the method, Mockito will then call the real method. If you want to Spy on or change the method, then you should stub it out.
0 Comments