Getting started with Junit on strauss ===================================== I've installed JUnit in /www/htdocs/CIS/software/dist/ The following links have helpful info: * http://junit.sourceforge.net/ also at: http://www.udel.edu/CIS/software/dist/junit3.8.1/aboutJunit.html * http://www.junit.org Configuring your account to work with JUnit on strauss ====================================================== Add the following to your .localenv file to set your classpath to include the standard libraries plus the JUnit library. setenv CLASSPATH . if -d $JAVA_HOME/lib then setenv CLASSPATH $JAVA_HOME/jre/lib:$JAVA_HOME/lib:$CLASSPATH endif if -d $JAVA_HOME/jre/lib then setenv CLASSPATH $JAVA_HOME/jre/lib:$JAVA_HOME/lib:$CLASSPATH endif if -f /www/htdocs/CIS/software/dist/junit3.8.1/junit.jar then setenv CLASSPATH /www/htdocs/CIS/software/dist/junit3.8.1/junit.jar:$CLASSPATH endif Making sure Junit is working ============================ JUnit contains some tests of itself to make sure it is working properly. That is, you can "run Junit on itself"! To run these tests, do the following: (1) Assuming "." is in your classpath, cd into the directory: cd /www/htdocs/CIS/software/dist/junit3.8.1 (2) Run the following to see Junit at work. The graphic versions will show you the famous "red/green" (hopefully all green!) for the batch TestRunner type: java junit.textui.TestRunner junit.samples.AllTests for the graphical TestRunner type: java junit.awtui.TestRunner junit.samples.AllTests for the Swing based graphical TestRunner type: java junit.swingui.TestRunner junit.samples.AllTests (This information came from the page: http://www.udel.edu/CIS/software/dist/junit3.8.1/aboutJunit.html#Installation ) Now that it is working, go to work on your OWN tests ==================================================== The article: http://www.udel.edu/CIS/software/dist/junit3.8.1/doc/testinfected/testing.htm is a good starting point. (This file is also available at the original site: http://junit.sourceforge.net/doc/testinfected/testing.htm) The classes from that article are in the "money" subdirectory. * The file Money.java is the class we are developing. * The file MoneyTest.java is the class to test the Money.java class. * We can run the tester by doing: -- text version (the only one you can run if you aren't on an XTerm) java junit.textui.TestRunner MoneyTest -- graphical awt version java junit.awtui.TestRunner MoneyTest -- graphical swing version java junit.swingui.TestRunner MoneyTest The swing version is the most interesting because you can see a list of all the tests in the MoneyTest class. *** New information, added 03/08/06 *** As a reminder, when a class is in a "package", this affects what your "path" must be to the class file involved. The sample file MoneyTest.java contains the line of source code: package junit.samples.money; So to run JUnit on the MoneyTest class as currently coded, you need to be in a directory that is three levels up from where the class file is located. For example, look at the following transcript from strauss showing a correct execution of JUnit on the sample code, and the explanation that follows it. > pwd /www/htdocs/CIS/software/dist/junit3.8.1 > echo $CLASSPATH /www/htdocs/CIS/software/dist/junit3.8.1/junit.jar:/usr/jdk1.5/jre/lib:/usr/jdk1.5/lib:/usr/jdk1.5/jre/lib:/usr/jdk1.5/lib:. > java junit.textui.TestRunner junit.samples.money.MoneyTest ...................... Time: 0.024 OK (22 tests) > Note that the directory /www/htdocs/CIS/software/dist/junit3.8.1 (shown as the current working directory by the pwd command in the example below) contains a subdirectory junit/samples/money/ that corresponds to the package directive in the source code: package junit.samples.money; That same package directive is also in the other .java files in that directory. If you aren't familiar with the concept of a "package" in Java, and what it means for your CLASSPATH settings, and your current working directory, go back to your CISC370 notes and review these concepts. :-) ****** End of new material from 03/08/06 ******** Other Resources =============== Web sites (freely available) * JUnit Primer: http://clarkware.com/articles/JUnitPrimer.html Written by Mike Clark, maintainer of the JUnit FAQ * JUnit FAQ The JUnit FAQ just mentioned. * A Dozen Ways to get the testing bug: http://today.java.net/pub/a/today/2004/01/22/DozenWays.html Also by Mike Clark; a great article to read over _just before_ you sit down with an empty editor screen to start writing some code. It shows you how to change your mindset to adopt the test-driven development way of thinking. Books (available with an O'Reilly Safari Subscription) * Java Extreme Programming Cookbook (Burke and Coyner O'Reilly, 2003, ISBN: 0-596-00387-0) Has an entire chapter on using JUnit. * Better, Faster, Lighter Java (Tate and Gehtland, O'Reilly 2004) pp. 26-35 introduce Junit, and describe how to integrate it with Ant They also put Junit in the context of an overall strategy of "keeping it simple" when doing Java development.