During the course of discussions in the office where I've been advocating the use of JUnit and test driven development, someone said "How do you write a JUnit test that verifies the standard 'Hello World' program?".
I must admit that I was stumped, which lost me a few points in the argument. Presumably, in the test there must be a way of diverting the output of System.out (without touching the HelloWorld class) to allow a String comparison to take place. So that I can regain some credibility in the office, could someone give me a clue.
For purposes of this posting we shall refer to the person who said "How do you write a JUnit test that verifies the standard 'Hello World' program?" as Mr. SmartAss.
Step 1: Say to Mr. SmartAss, "Nobody likes a smart ass".
Step 2: Rewrite the Hello World program as follows
Step 3: You should be able to write a unit test the program now. I know. I know. I did rewrite the program. But that is the point of unit testing - the creation of better code.
mmm. Your change does mean that it's now simple to establish what message will be sent for printing but not that it is printed.
After posting the original question, I downloaded Bruce Eckel's Java book and noticed that in his chapter on testing he focusses precisely on establishing a testing mechanism that captures the System.out.println() output. This enables him to test the book's sample code. However, it runs to several pages, so it's going to take a while for me to get my head around it and hone it down to something whose complexity is of the same order as the problem it addresses.