From: Keith H. <kh...@ax...> - 2006-04-04 08:23:34
|
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. =20 Hope this helps Keith =20 =20 -----Original Message----- From: jun...@li... [mailto:jun...@li...] On Behalf Of Janette Wong Sent: 03 April 2006 21:20 To: jun...@li... Subject: [Junit-devel] How do I check for output from a method sent to the console via System.out.println? =09 =09 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.=20 =09 My question is on testing a couple of Strings sent to the console. e.g. I have this method:=20 =09 public static void usage() {=20 System.out.println("Usage: BookStoreServer employeeID [seed]");=20 System.out.println("where employeeID is from E0000 to E9999");=20 }=20 =09 The usage() method is called under some circumstances.=20 =09 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=20 check that those message have indeed been sent to the console.=20 =09 public void testStartServerNumOfArgsMoreThanUpperBound(){=20 String[] argsArray =3D {"E0001", "1234", "invalid3rdArgument"}; =20 StartServer.main(argsArray);=20 //assertEquals("Usage: BookStoreServer employeeID [seed]"); // Have to figure out how to assert the output from usage()=20 }=20 =09 =09 Appreciate any guidance, thanks.=20 =09 =09 Janette Wong=20 =09 =09 =09 =09 =09 |