> For the complete documentation index, see [llms.txt](https://notes.tejpratapsingh.com/_/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.tejpratapsingh.com/_/java-testing/mockito/mockito.spy.md).

# Mockito.spy()

It is similar to *`mock()`* but instead of return default value it will call the actual implementation when ***not*** mocked.

```java
// with annotation
@Spy
private ClassName spy;

// Or with static method
final ClassName spy = Mockito.spy(ClassName.class);

// mock methods that returns something
Mockito.doReturn(value).when(spy).method();

// mock method to throw exception
Mockito.doThrow(new ExceptionClass()).when(spy).metohd();

// mock void methods
Mockito.doNothing().when(spy).method();
```
