Académique Documents
Professionnel Documents
Culture Documents
Summary
Table of Contents
• Introduction
• Why Use JUnit?
• Design of JUnit
• Step 1: Install JUnit
• Step 2: Write a Test Case
• Step 3: Write a Test Suite
• Step 4: Run the Tests
• Step 5: Organize the Tests
• Testing Idioms
• Training and Mentoring
• Resources
The fewer tests you write, the less stable your code
becomes. Tests validate the stability of the software and
instill confidence that changes haven't caused a ripple-
effect through the software. The tests form the glue of
the structural integrity of the software.
• JUnit is free!
Design of JUnit
JUnit is designed around two key design patterns: the
Command pattern [Command Pattern: An object encapsulates
everything needed to execute a method in another object.]
and the Composite pattern[Composite Pattern: Assemble
groups of objects with the same signature.].
A TestCase is a command object. Any class that contains test
methods should subclass the TestCase class. A TestCase can
define any number of public testXXX() methods. When you want
to check the expected and actual test results, you invoke a
variation of the assert() method.
TestCase subclasses that contain multiple testXXX() methods can
use the setUp() and tearDown() methods to initialize and release
any common objects under test, referred to as the test fixture.
Each test runs in the context of its own fixture, calling setUp()
before and tearDown() after each test method to ensure there
can be no side effects among test runs.
TestCase instances can be composed into TestSuite hierarchies
that automatically invoke all the testXXX() methods defined in
each TestCase instance. A TestSuite is a composite of other tests,
either TestCase instances or other TestSuite instances. The
composite behavior exhibited by the TestSuite allows you to
assemble test suites of test suites of tests, to an arbitrary
depth, and run all the tests automatically and uniformly to
yield a single pass or fail status.
1. First, download
[http://sourceforge.net/project/showfiles.php?group_id=
15278] the latest version of JUnit, referred to below as
junit.zip.
2. Then install JUnit on your platform of choice:
Windows
set CLASSPATH=%JUNIT_HOME%\junit.jar
Unix (bash)
To install JUnit on Unix, follow these steps:
export CLASSPATH=$JUNIT_HOME/junit.jar
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp() {
cart.addItem(book1);
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
protected void tearDown() {
// release objects under test here, if
necessary
}
/**
* Tests emptying the cart.
*/
public void testEmpty() {
cart.empty();
assertEquals(0, cart.getItemCount());
}
/**
* Tests adding an item to the cart.
*/
public void testAddItem() {
double expectedBalance =
book1.getPrice() + book2.getPrice();
assertEquals(expectedBalance,
cart.getBalance(), 0.0);
assertEquals(2, cart.getItemCount());
}
/**
* Tests removing an item from the cart.
*
* @throws ProductNotFoundException If the
product was not in the cart.
*/
public void testRemoveItem() throws
ProductNotFoundException {
cart.removeItem(book1);
assertEquals(0, cart.getItemCount());
}
/**
* Tests removing an unknown item from the
cart.
*
* This test is successful if the
* ProductNotFoundException is raised.
*/
public void testRemoveItemNotInCart() {
try {
fail("Should raise a
ProductNotFoundException");
} catch(ProductNotFoundException
expected) {
// successful test
}
}
}
1. setUp()
2. testXXX
3. tearDown()
//
// The ShoppingCartTest we created
above.
//
suite.addTestSuite(ShoppingCartTest.class
);
//
// Another example test suite of tests.
//
suite.addTest(CreditCardTestSuite.suite());
//
// Add more tests here
//
return suite;
}
/**
* Runs the test suite using the textual
runner.
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
The textual user interface displays "OK" if all the tests passed
and failure messages if any of the tests failed.
To run the test case using the graphical user interface, use:
Testing Idioms
Keep the following things in mind when writing JUnit tests:
• The software does well those things that the tests check.
• Test a little, code a little, test a little, code a little...
• Make sure all tests always run at 100%.
• Run all the tests in the system at least once per day (or
night).
• Write tests for the areas of code with the highest
probability of breakage.
• Write tests that have the highest possible return on your
testing investment.
• If you find yourself debugging using System.out.println(),
write a test to automatically check the result instead.
• When a bug is reported, write a test to expose the bug.
• The next time someone asks you for help debugging,
help them write a test.
Write unit tests before writing the code and only write new
code when a test is failing.
Resources