The Artima Developer Community
Sponsored Link

Java Answers Forum
JUnit and Hello World

3 replies on 1 page. Most recent reply: May 23, 2003 7:19 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

JUnit and Hello World Posted: May 21, 2003 3:36 AM
Reply to this message Reply
Advertisement
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?".
public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello world.");
    }
}
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.

Thanks,
Vince.


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: JUnit and Hello World Posted: May 21, 2003 4:52 AM
Reply to this message Reply
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
public class HelloWorld 
{
    public String greeting() 
    {
        return "Hello world!";  
    }
 
    public static void main( String args[] )
    {
        System.out.println( greeting() );
    }
}


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.

Best of luck,
Adam

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: JUnit and Hello World Posted: May 22, 2003 2:49 AM
Reply to this message Reply
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.

Vince.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: JUnit and Hello World Posted: May 23, 2003 7:19 PM
Reply to this message Reply
I agree with Adam's step 1; that is vital (or you can refer Mr. S to this forum thread if you want to avoid getting in trouble for name-calling).

Wouldn't it be a simple matter of setting System.out to your own PrintStream that does the checking? Use System.setOut() to do this.

By the way, you can also do the same with System.in and System.err, for more advanced testing of console apps.

Flat View: This topic has 3 replies on 1 page
Topic: Java gets the system property ???? Previous Topic   Next Topic Topic: JUnit and JDBC connections

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use