Test Cases Made Easy with JUnit 4.5
Running the Test Cases
There are two ways to run JUnit test cases. The first is to invoke a test runner at the command line, as shown by the following example:
$%JAVA_HOME%\bin\java -cp "%classpath -ea org.junit.runner.JUnitCore com.dlt.developer.junit. NumberCruncherTestSuite45
Note that all test runners in JUnit 4.5 are text-based; the old Swing version that was available previously is now deprecated.
<target name="test45" depends="compile-test"> <junit> <classpath refid="classpath.test" /> <formatter type="plain" usefile="false" /> <test name="com.dlt.developer.junit. NumberCruncherTestSuite45" /> </junit> </target>
This Ant target runs the JUnit 4.5 test cases using the NumberCruncherTestSuite45.java class discussed earlier. The output is plain text that is echoed to the console. There are many fancier options for saving the output of extensive test suites to various output formats, such as XML, and even for applying XSL stylesheets to the output for control over the appearance of the report that is generated.
The target for JUnit 3.8 is nearly identical, except of course that the test suite class is NumberCruncherTestCase38.java. The build.xml file that is included with the source code in the Resources section has a target for running the JUnit 4.5 examples (target "test45"), the JUnit 3.8 examples (target "test38"), and for running both (target "all"). Remember that you will need to download Ant version 1.7.1 or later to use the <junit> tag to run JUnit 4.5 test suites.
Now, you should be familiar with the new features of JUnit 4.5, and should have a good idea of how JUnit 4.5 differs from the old, tried-and-true JUnit 3.8. The new features and more flexible architecture of JUnit 4.5 make an already invaluable tool even better.
For a good discussion of software testing in general, see "Software Testing for Programmers, Part 1," and for a detailed introduction to the features of JUnit 3.8, see "Software Testing for Programmers, Part 2."
JUnit Documentation and Downloads
The complete code examples from this article and the Ant script to execute them can be downloaded here.
About the Author
David Thurmond is a Sun Certified Developer with over fifteen years of software development experience. He has worked in the agriculture, construction equipment, financial, home improvement and logistics industries.
Page 5 of 5