Unit Testing
Unit Testing
/**
* Constructs a ShoppingCartTest with the specified name.
*
* @param name Test case name.
*/
public ShoppingCartTest(String name) {
super(name);
}
/**
* Sets up the text fixture.
*
* Called before every test case method.
*/
protected void setUp() {
_bookCart = new ShoppingCart();
/**
* Tears down the text fixture.
*
* Called after every test case method.
*/
protected void tearDown() {
_bookCart = null;
}
/**
* Tests the emptying of the cart.
*/
public void testEmpty() {
_bookCart.empty();
assert(_bookCart.isEmpty());
}
/**
* Tests adding a product to the cart.
*/
public void testProductAdd() {
assertEquals(expectedBalance, currentBalance,
tolerance);
int expectedItemCount = 2;
int currentItemCount = _bookCart.getItemCount();
assertEquals(expectedItemCount, currentItemCount);
}
/**
* Tests removing a product from the cart.
*
* @throws ProductNotFoundException If the
* product was not in the cart.
*/
public void testProductRemove() throws
ProductNotFoundException {
assertEquals(expectedBalance, currentBalance,
tolerance);
int expectedItemCount = 0;
int currentItemCount = _bookCart.getItemCount();
assertEquals(expectedItemCount, currentItemCount);
}
/**
* Tests removing an unknown product from the cart.
*
* This test is successful if the
* ProductNotFoundException is raised.
*/
public void testProductNotFound() {
try {
Product book = new Product("Ender's Game",
4.95);
_bookCart.removeItem(book);
fail("Should raise a
ProductNotFoundException");
} catch(ProductNotFoundException pnfe) {
// successful test
}
}
/**
* Assembles and returns a test suite for
* all the test methods of this test case.
*
* @return A non-null test suite.
*/
public static Test suite() {
//
// Reflection is used here to add all
// the testXXX() methods to the suite.
//
TestSuite suite = new
TestSuite(ShoppingCartTest.class);
//
// Alternatively, but prone to error when adding more
// test case methods...
//
// TestSuite suite = new TestSuite();
// suite.addTest(new ShoppingCartTest("testEmpty"));
// suite.addTest(new
// ShoppingCartTest("testProductAdd"));
// suite.addTest(new
// ShoppingCartTest("testProductRemove"));
// suite.addTest(new
// ShoppingCartTest("testProductNotFound"));
//
return suite;
}
/**
* Runs the test case.
*
* Uncomment either the textual UI, Swing UI, or
AWT UI.
*/
public static void main(String args[]) {
String[] testCaseName =
{ShoppingCartTest.class.getName()};
//junit.textui.TestRunner.main(testCaseName);
//junit.swingui.TestRunner.main(testCaseName);
junit.ui.TestRunner.main(testCaseName);
}
}
Step 2: Write a Test Suite
/**
* Constructs a EcommerceTestSuite with the specified name.
*
* @param name Test suite name.
*/
public EcommerceTestSuite(String name) {
super(name);
}
/**
* Assembles and returns a test suite
* containing all known tests.
*
* New tests should be added here!
*
* @return A non-null test suite.
*/
public static Test suite() {
//
// The ShoppingCartTest we created above.
//
suite.addTest(ShoppingCartTest.suite());
//
// Another example test suite of tests.
//
suite.addTest(CreditCartTestSuite().suite());
return suite;
}
/**
* Runs the test suite.
*
* Uncomment either the textual UI, Swing UI, or AWT UI.
*/
public static void main(String args[]) {
String[] testCaseName = {EcommerceTestSuite.class.getName()};
//junit.textui.TestRunner.main(testCaseName);
//junit.swingui.TestRunner.main(testCaseName);
junit.ui.TestRunner.main(testCaseName);
}
}
Step 3: Run the Tests
• Now that we've written a test
suite containing a collection of
test cases and other test suites,
we can run either the test suite
or any of its test cases
individually
• Running a TestSuite will
automatically run all of its
subordinate TestCase instances
and TestSuite instances.
Running a TestCase will
automatically invoke all of its
defined testXXX() methods
Step 4: Organize the Tests
1. Create test cases in the same package as the
code under test. For example, the
com.mydotcom.ecommerce package would
contain all the application-level classes as
well as the test cases for those components.
If you want to avoid combining application
and testing code in your source directories,
it's recommended to create a parallel,
mirrored directory structure that contains
the test code.
2. For each Java package in your application,
define a TestSuite class that contains all the
tests for verifying the code in the package.
3. Define similar TestSuite classes that create
higher-level and lower-level test suites in
the other packages (and sub-packages) of
the application.
4. Make sure your build process includes the
compilation of all test suites and test cases.
This helps to ensure that your tests are
always up-to-date with the latest code and
keeps the tests fresh.
Testing Idioms
Keep these things in mind when testing: