JUnit


Once you have successfully compiled StudentTest, you can execute it in JUnit. JUnit provides two GUI-based interfaces and a text interface. Refer to the JUnit documentation for further information. The following command will execute the AWT interface[3] against StudentTest.class, using JUnit's class named junit.awtui.TestRunner.

[3] The AWT is Java's more bare-boned user interface toolkit, in contrast with Swing, which provides more controls and features. The AWT version of the JUnit interface is simpler and easier to understand. To use the Swing version, use the class name junit.swingui.TestRunner instead of junit.awtui.TestRunner. To use the text version, use the class name junit.textui.TestRunner.

 java -cp .;c:\junit3.8.1\junit.jar junit.awtui.TestRunner StudentTest 

You once again specify the classpath, this time using the abbreviated keyword -cp. Not only does the Java compiler need to know where the JUnit classes are, but the Java VM also needs to be able to find these classes at runtime so it can load them up as needed. In addition, the classpath now contains a ., representing the current directory. This is so Java[4] can locate StudentTest.class: If a directory is specified instead of a JAR filename, Java scans the directory for class files as necessary.

[4] You'll note I use phrases such as "Java does this" often. This is a colloquial (i.e., lazy) way of saying "The Java virtual machine does this," or "The Java compiler does that." You should be able to determine whether I'm talking about the VM or the compiler from the context.

The command also contains a single argument, StudentTest, which you pass to the junit.awtui.TestRunner class as the name of the class to be tested.

When you execute the TestRunner, you should see a window similar to Figure 1.3.

Figure 1.3. JUnit (showing a red bar)


There really isn't very much to the JUnit interface. I will discuss only part of it for now, introducing the remainder bit by bit as appropriate. The name of the class being tested, StudentTest, appears near the top in an entry field. The Run button to its right can be clicked to rerun the tests. The interface shows that you have already executed the tests once. If you click the Run button (go ahead!), you will see a very quick flash of the red bar[5] spanning the width of the window.

[5] If you are color-blind, the statistics below the bar will provide you with the information you need.

The fact that JUnit shows a red bar indicates that something went wrong. The summary below the red bar shows that there is one (1) failure. The Errors and Failures list explains all the things that went wrong; in this case, JUnit complained because there were "No tests found in StudentTest." Your job as a test-driven programmer will be to first view errors and failures in JUnit and then quickly correct them.



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