Talk:CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest
Step 1: Installing JUnit
1. Download latest version of JUnit from http://sourceforge.net/projects/junit/files/ referred as “referred to below as junit.zip”.
2. Then install JUnit on your platform of choice:
Windows
To install JUnit on Windows, follow these steps:
1. Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.
2. Add JUnit to the classpath:
set CLASSPATH=%JUNIT_HOME%\junit.jar
Unix (bash)
To install JUnit on Unix, follow these steps:
1. Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.
2. Add JUnit to the classpath:
export CLASSPATH=$JUNIT_HOME/junit.jar
Step 2: Writing a Test Case
To write a test case, follow these steps: 1. Implement a class derived from TestCase, a base class provided by JUnit.
public class ShoppingCartTest extends TestCase
2. Be sure the test class belongs to the same package as that of the unit you wish to test 3. Override the setUp() method to initialize object(s) under test. 4. Optionally override the tearDown() method to release object(s) under test. 5. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. The following is an example test case:
import junit.framework.TestCase; public class ShoppingCartTest extends TestCase {
private ShoppingCart cart1; private ShoppingCart cart2;
/** * Sets up the test fixture. * * Called before every test case method. */ protected void setUp() {
cart1 = new ShoppingCart(6,8); cart2 = new ShoppingCart(6,8); }
/** * 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() {
cart1.empty(); cart2.empty(); assertEquals(cart1, new ShoppingCart(3, 9)); assertTrue(!cart2.equals(cart1)); } }
Methods used in JUnit: 1. An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails 2. static void assertTrue(boolean test) static void assertTrue(String message, boolean test) a. Throws an AssertionFailedError if the test fails b. The optional message is included in the Error 3. static void assertFalse(boolean test) static void assertFalse(String message, boolean test) a. Throws an AssertionFailedError if the test fails
Step 3: Writing a Test Suite
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop. To write a test suite, follow these steps: 1. Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. 2. Optionally define a main() method that runs the TestSuite in batch mode. The following is an example test suite: dfdf
import junit.framework.Test;
import junit.framework.TestSuite;
public class ShoppingCartTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
//
// 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());
}
}
Step 4: Running the Test
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 public testXXX() methods. JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. To run our test case using the textual user interface, use: java junit.textui.TestRunner ShoppingCartTest 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: java junit.swingui.TestRunner ShoppingCartTest The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed. The ShoppingCartTest Suite can be run similarly: java junit.swingui.TestRunner ShoppingCartTestSuite