Have you ever had a method that looks like this, where the overvable output is a static call to System.out.println(), and wondered how to test it?
You want to verify that when you pass in the String “Tim” to the nice() method you get “Hello Tim, nice to see you.” printed to the terminal. But, how the heck do you sense what’s being printed to the terminal?
Wrap the calls to System.out in an instance class, inject the wrapper class into the class under test, then use a class mocking test tool to capture the output. For example with jUnit and Mockito:
You can employ this general technique to observe calls to static methods in any class.