The Best Way To Test Logger Methods Were Called Using Mockito Spies

Опубликовано: 30 Март 2025
на канале: Tales from the jar side
1,516
30

How to use spies in Mockito to check that logging methods are invoked as expected. The demo is a class that does business logic and calls the info method on a logger. Mockito is used to verify that the info method is invoked exactly once with the proper argument.

Additional code is used to capture standard output and standard error, but because of the spy, it’s really not necessary to do all that extra work.

Mockito Made Clear: https://pragprog.com/titles/mockito/m...
Tales from the jar side newsletter: https://kenkousen.substack.com
Tales from the jar side videos:    / @talesfromthejarside  
GitHub repository: https://github.com/kousen/mockitobook


00:00 - Welcome
00:55 - Spies in Mockito
04:41 - Creating a Mockito spy
06:21 - Capturing standard out and err
08:57 - Is capturing output necessary?