You could capture standard output (using System.setOut(...)) into your own OutputStream - e.g. to a file or ByteArrayOutputStream, then check the content of that stream. Just make sure you hold the original System.out somewhere and reset it in a finally block.
Hope this helps
-----Original Message-----
From: [] On Behalf Of Janette Wong
Sent: 03 April 2006 21:20
Subject: [Junit-devel] How do I check for output from a method sent to the console via System.out.println?

Hi, my apologies if this question appears twice... I sent it once to the mailing list and found out I have to subscribe to the list first.   So I subscribed and here's my question again.

My question is on testing a couple of Strings sent to the console.   e.g. I have this method:

        public static void usage() {
                System.out.println("Usage: BookStoreServer employeeID [seed]");
                System.out.println("where employeeID is from E0000 to E9999");

The usage() method is called under some circumstances.

I wrote a JUnit test (see snippet below).   When StartServer.main() is called with 3 arguments, it should cause the usage() method above to be invoked and output messages to the console.  Question is, I don't know how to use assertEquals() or other means to
check that those message have indeed been sent to the console.

        public void testStartServerNumOfArgsMoreThanUpperBound(){
                String[] argsArray = {"E0001", "1234", "invalid3rdArgument"};                
                //assertEquals("Usage: BookStoreServer employeeID [seed]");   // Have to figure out how to assert the output from usage()

Appreciate any guidance, thanks.

Janette Wong