|home page - news - syllabus - schedule - assignments - tutorials - java - references - Mike Jacobson|
|basics - debugger (jdb) - testing framework (JUnit) - profiler (VisualVM) - 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 — 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.12, 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
in 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 and Install” on the left of the JUnit.org home page). Download junit.jar, and hamcrest-core.jar, and move them 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/share/java 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 files into /usr/share/java as suggested above, then you should make sure that
/usr/share/java/hamcrest-core.jarare 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
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. In particular, the following is recommended:
Getting Started: A tutorial in which you are guided through the process of writing and executing a test suite using JUnit.
Note that this tutorial assumes that both .jar files and the source files are all in the same directory. Ff you have followed the instructions to install JUnit then some instructions can be simplified. In particular, you do not need to use the -cp flag when using the javac or java commands provided that your CLASSPATH. has been modified correctly. In the section entitled “Create a Test,” you should be able to replace the command
javac -cp .:junit-4.XX.jar CalculatorTest.java
with the simpler command
and you should also be able to replace the command
java -cp .:junit-4.XX.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest
with the simpler command
java org.junit.runner.JUnitCore CalculatorTest
A self study exercise, designed to help you to write and use JUnit test suites is now available.