in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Java Tools for Extreme programming mastering open source tools including - Hightower R.

Hightower R. Java Tools for Extreme programming mastering open source tools including - Wiley publishing , 2002. - 417 p.
ISBN: 0-471-20708
Download (direct link): javatoolsforextremeprog2002.pdf
Previous << 1 .. 51 52 53 54 55 56 < 57 > 58 59 60 61 62 63 .. 159 >> Next

public void testContainsKey(){
assertTrue("It should contain the apple key",
Or we can opt to leave it out, as follows: public void testContainsValue(){ assertTrue(testMap.containsKey(APPLE_VALUE));
Note that the setUp() and tearDown() methods are called before and after every textXO method that is run. Because the setUp() method does not allocate any resources that need to be released, the HashMapTest does not need to override the tearDown() method. If it did, the code would look something like this:
protected void setUp() {
//get db connection
connection = DriverManager.getConnection();
statement = connection.createStatement();
results = statement.executeQuery("select count(*) from Pet");
protected void tearDown() {
//get db connection results.close(); statement.close(); connection.close();
So far, we've created a test case and fixture objects and tested with assert, but how do we group these test cases into a suite of related test cases? The authors of JUnit have also provided an example of how to do this. They define two tests in the JUnit samples directory: VectorTest and MoneyTest. Then, they define a test suite to run the test cases in the class AllTest, defined in Listing 7.2.
Listing 7.2: AllTest Test Suite Setup.
package junit.samples;
import junit.framework.*;
* TestSuite that runs all the sample tests
public class AllTests {
public static void main (String[] args) { (suite());
public static Test suite ( ) {
TestSuite suite= new TestSuite("All JUnit Tests"); suite.addTest(VectorTest.suite()); suite.addTest (new TestSuite(MoneyTest.class)); suite.addTest(junit.tests.AllTests.suite()); return suite;
The code in Listing 7.2 compiles several suites of test into one suite. Notice that the main method calls, passing it the returned value from the static suite() method. The suite() method creates an instance of Test-Suite and then adds suites of tests from VectorTest, MoneyTest, and junit.tests.AllTests. Notice that when the AllTest suite() method adds the VectorTest, it calls the VectorTest's suite() method, which is defined as follows:
public static Test suite() {
return new TestSuite(VectorTest.class);}
As you can see, the VectorTest's static suite() method creates a new TestSuite instance by passing itself as a class. TestSuite uses reflection to extract the test methods that make up the suite. The end effect is that you can group related tests into larger and larger test suites. Thus, you could have suites of tests nested in a larger suite. The alternative would be to run every TestCase independently, which would take a long time and would be tedious. Nesting suites of tests enables you to test large portions of code quickly.
We have now covered the basics of JUnit. In the next section, we integrate the test suite in Listing 7.2 with Ant.
Integrating JUnit with Ant
JUnit and Ant go together like a horse and carriage. Ant automates the build-and-deploy process. JUnit automates testing. Put them together, and Ant can automate the build, deploy, and test process. Ant has several tags to support JUnit.
For the integration to work, we need the Extensible Style Language Transformation (XSLT) transform engine JAR file installed; refer to the Ant User Manual documentation or Chapter 12, "Ant Tag Reference" for more information. We also need to put the JAR file for JUnit on the Ant classpath, and we must download the optional.jar file from the Apache site (go to ant/index.html and select the "download" option). The easiest way to put these JAR files on the Ant classpath is to copy them to the lib directory in the ANT_HOME directory (ANT_HOME/lib).
Once we have the required JAR files, we can build and test the last example with the following Ant buildfile, which we put in the ANT_HOME directory:
<project name="junitSample" default="test">
<target name="init">
<property name="outdir" value="/tmp/junitSample" /> </target>
<target name="prepare" depends="init">
<mkdir dir="${outdir}" />
<target name="compile" depends="prepare">
<javac srcdir="." destdir="${outdir}" classpath="junit.jar"/>
<target name="test" depends="compile">
<junit printsummary="true" >
<test name="junit.samples.AllTests" />
<pathelement location="${outdir}" /> </classpath>
Let's quickly break down this buildfile. The name of the project is junitSample, and it has the typical targets, as follows: init, prepare, compile, and test. The test target is the default target of the junitSample project's buildfile. The init target creates an "outdir" property that holds the location of the output directory. The prepare tag creates the output directory (outdir). The compile tag builds the Junit sample source code (discussed in the last section) to the output directory (outdir). The interesting target is the test target, as follows:
Previous << 1 .. 51 52 53 54 55 56 < 57 > 58 59 60 61 62 63 .. 159 >> Next