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.
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.
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.
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. |