The way for testing this its make a good design with a "output" abstraction instead of print directly to "system.out". For example:
interface Output {
void print(String data);
}
Then you inject this dependency into the class that needs to send information to the output:
class MyProgram {
private Output out;
public MyProgram(Output output) {
this.out = output;
}
public doSomething() {
// do something
out.print("i do something!);
}
}
Then you can test easily with a mock (with mockito for example):
public void test_my_program() {
Output mockOutput = mock(Output.class);
MyProgram myProgram = new MyProgram(mockOutput);
myProgram.doSomething();
verity(mockOutput).print("do something!");
}
I don't verify the code but its more or less correct and i expect enough for you to get the idea. Always its the same, testing its not difficult, the difficult thing its design good testable code, here we are only using abstraction and dependency injection, two basic principles of OO.
The real advantage of this code its that now its not tied to "System.out", you can use this class in a web application for example if you want. For example making and output implementation that talks to the web client via websocket. This is another OO principle, your class is now "Open-Close", you can add functionality (websocket crazy example :P) without modify your actual code.