Verbose mode on spies in Mockito

Viewed 351

When creating normal mocks you can make the mock verbose for easier debugging, e.g.:

 List mockWithLogger = mock(List.class, withSettings().verboseLogging());

Example taken from Mockito documentation. How can I achieve similar result when using spies? I.e. I would like to write something like:

List spyWithLogger = spy(new MyList(), withSettings().verboseLogging());

but it doesn't seem to compile.

1 Answers

Looking at the implementation of spy the following should work:

MyList myList = new MyList();
List spyWithLogger = mock(myList.getClass(), withSettings()
                                             .verboseLogging()
                                             .spiedInstance(myList)
                                             .defaultAnswer(CALLS_REAL_METHODS));
Related