home page - news - syllabus - schedule - assignments - tutorials - tests - java - references - Mike Jacobson |
basics - debugger (jdb) - testing framework (JUnit) - profiler (JRat) - documentation (javadoc) |
Java Resources - Testing Framework (JUnit) |
JUnit is a widely used “test framework,” that is, a software product that helps you to write test cases, organize the test cases into test suites, run the test suites and report the results. You will be expected to use this tool in CPSC 331.
There is quite a bit of information about JUnit available. Unfortunately this information is not always very readable. Furthermore, there are significant differences between version 3 of JUnit — which is used in a case study and tutorial in the textbook — and version 4, which is available at school and which we will be using. It is not always clear in documentation which version of JUnit is being discussed, and this can make things even more confusing.
All the information that is presented below concenrns JUnit 4.5, the version that is currently available at the Department of Computer Science.
If you wish to use JUnit when using your computer science account at school, then all you need to do is include
/usr/lib/junit4.5/junit-4.5.jar
on your CLASSPATH.
It is possible that JUnit is already installed on your system at home — but it is quite likely that the version you have at home is somewhat out of date. The latest version can be downloaded from JUnit.org (follow the link to “Download JUnit” at the top of the JUnit.org home page). You will see several different files to choose from; I recommend that you select the file junit4.5.zip, because this includes useful documentation along with the JUnit tool. (Of course you can install a more recent version, but you should make sure that your code can be compiled and executed at school.)
Once you have downloaded the above file and decompressed it, a directory named junit4.5 that contains a file called junit-4.5.jar should be available. Move this to a directory where you would like JUnit to be stored: If you are using a Macintosh or running Linux at home (and you have administration privileges) then /usr/lib would be a reasonable choice. If you are running a recent version of Windows then C:\Program Files\Java might be a reasonable choice, instead.
Finally, you should modify your CLASSPATH so that includes JUnit. If you are using a Macintosh or running Linux, and you moved the directory junit4.5 into /usr/lib as suggested above, then you should make sure that
/usr/lib/junit4.5/junit-4.5.jar
is on your CLASSPATH. On the other hand, if you are running a recent version of Windows and you followed the above suggestion about where to move the directory, then you should make sure that
C:\Program Files\Java\junit4.5\junit-4.5.jar
is part of your CLASSPATH instead.
Note: If are using Windows at home and you have been working through the self-study exercises that are now available then you have probably already modified your PATH environment variable in order to be able to compile and run Java programs from the command line within (what is essentially) a terminal window. The process of modifying your CLASSPATH should be similar, except that the CLASSPATH environment variable might not exist yet, so that you will have to add (or “create”) it.
Note: JUnit is now built into several IDE’s, including both Eclipse and NetBeans. Note, however, that the version of JUnit that is built into these is not necessarily up-to-date: It may therefore be worthwhile to follow the above instructions so that you have access to the same version of JUnit at home and at school.
Useful information about how to write and execution JUnit can be found at the JUnit.org web site. Both of the following documents were found here and are recommended.
JUnit 4.x Quick Tutorial: A tutorial in which you are guided through the process of writing and executing a test suite using JUnit.
Note that if you have followed the instructions to install JUnit then parts of this can be skipped and some instructions can be simplified. In particular, you can safely skip the section entitled “Locate it first,” and you do not need to use the -cp flag when using the javac or java commands. In particular, in the section entitled “Compiling and executing your test,” you should be able to replace the command
javac –cp .:<full path to JUnit.jar> SubscriptionTest.java
with the simpler command
javac SubscriptionTest.java
and you should also be able to replace the command
java –cp .:<full path to JUnit.jar> org.junit.runner.JUnitCore SubscriptionTest
with the simpler command
java org.junit.runner.JUnitCore SubscriptionTest
JUnit4 in 60 Seconds: A good document to read after the above: It skips some details but provides information about additional features provided by JUnit that are potentially quite useful.
A self study exercise, designed to help you to write and useJUnit test suites is now available.
Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/Courses/cpsc331/W12/java/testing.html |