System.out


The geTReport method returns a String that contains a report of all the students enrolled in a course session. In the production student information system, the String isn't going to be of much use to anyone unless you print it out or display it somewhere. Java provides output facilities to allow you to redirect information to the console, to files, and to other destinations. You will learn about these output facilities in depth in Lesson 11.

In this exercise you will modify the test so that the report displays on the console. It isn't yet a requirement, but sometimes you need to be able to display things for various reasons. The next section goes into some of these reasons.

In the Setup section of this book, you coded and ran a "Hello World" application that printed text to your console. The line of code to print the text on the console was:

 System.out.println("hello world"); 

Look at the J2SE API documentation for the class named System, located in the package java.lang. You will see that out is a static variable, of the type PrintStream, that represents the standard output stream, also known as stdout or simply "the console." You can directly access this console object using the following static variable reference:

 System.out 

Once you have this console object, you may send it a number of messages, including the message println. The println method takes a String (among other things) and writes it to the underlying output stream.

Add a line to RosterReporterTest that displays the report on the console by using System.out:

 package studentinfo; import junit.framework.TestCase; public class RosterReporterTest extends TestCase {    public void testRosterReport() {       CourseSession session =          new CourseSession("ENGL", "101",              new DateUtil().createDate(2003, 1, 6));       session.enroll(new Student("A"));       session.enroll(new Student("B"));       String rosterReport = new RosterReporter(session).getReport(); System.out.println(rosterReport);       assertEquals(          RosterReporter.ROSTER_REPORT_HEADER +          "A" + RosterReporter.NEWLINE +          "B" + RosterReporter.NEWLINE +          RosterReporter.ROSTER_REPORT_FOOTER + "2" +          RosterReporter.NEWLINE, rosterReport);    } } 

Rerun your tests. You should see the actual report displayed onscreen. If you are running in an IDE, you may need to use System.err (the standard error output stream, also known as syserr), instead of System.out, in order to view the results.[5]

[5] The results might appear in a window named "console."

You'll note that I placed the additional line of code all the way to the left margin. I use this convention to remind me that the code is intended for temporary use. It makes such statements easy to locate and remove.

Revert these changes and rerun all tests once you have finished viewing the output.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net