6.3 Usage


This section presents a quick overview of basic JUnit usage. The xUnit examples in the previous chapters provide a more detailed review of how to use JUnit.

Test classes in JUnit are subclasses of TestCase . Tests can be built in a number of ways, but the conventional approach is described here. The name of the test class starts with the name of the object being tested and ends with Test . Test classes contain a separate test method for each behavior being tested. Test methods are named starting with test .

Test conditions are checked with test assert methods . Test asserts result in test success or failure. If the assert fails, the test method returns immediately. If it succeeds, the execution of the test method continues. Since a test method should only test a single behavior, in most cases, each test method should only contain one test assert.

If there are objects that are shared by the test methods, they should be initialized in the setUp() method and destroyed in tearDown( ) . These methods are called before and after each test method call, effectively recreating the test fixture for each test, thereby providing test isolation.

Example 6-1 shows a test class built following this model, called LibraryTest , that (naturally) tests the class Library .

Example 6-1. The test class LibraryTest
 LibraryTest.java import junit.framework.*; import java.util.*; public class  LibraryTest  extends TestCase {    private  Library  library;    public void  setUp  ( ) throws Exception {       library = new Library( );       library.addBook(new Book( "Cosmos", "Carl Sagan" ));       library.addBook(new Book( "Contact", "Carl Sagan" ));       library.addBook(new Book( "Solaris", "Stanislaw Lem" ));       library.addBook(new Book( "American Beauty", "Allen M Steele" ));       library.addBook(new Book( "American Beauty", "Edna Ferber" ));    }    public void  tearDown  ( ) {       library.empty( );       library = null;    }    public void  testGetBooksByTitle  ( ) {       Vector books = library.getBooksByTitle( "American Beauty" );  assertEquals  ( "wrong number of books found", 2, books.size( ) );    }    public void  testGetBooksByAuthor  ( ) {       Vector books = library.getBooksByAuthor( "Carl Sagan" );  assertEquals  ( "2 books not found", 2, books.size( ) );    }    public void  testEmpty  ( ) {       library.empty( );  assertEquals  ( "library not empty", 0, library.getNumBooks( ) );    } } 

LibraryTest is a test fixture because there are multiple test methods sharing an objectin this case, an instance of Library . The Library is created and loaded with a set of Book s in setUp( ) and emptied and disposed of in tearDown( ) .

Each test method verifies a distinct behavior of the Library class with a single test assert. Although the tests may modify the Library , as shown by testEmpty( ) , the test fixture functionality guarantees that each test runs in a clean fixture, without dependencies on the results of the others.

Tests are run using one of the TestRunner tools provided with JUnit. The simplest and most easily automated of these is TextTestRunner . Example 6-2 demonstrates using TextTestRunner to run LibraryTest .

Example 6-2. Running LibraryTest with the TextTestRunner
 $ java junit.textui.TestRunner LibraryTest ......... Time: 0.01 OK (3 tests) 

It often is useful to aggregate multiple tests so they can be run together. The class TestSuite is used to contain a collection of tests. Example 6-3 shows a class, LibraryTests , which creates a TestSuite containing a number of test classes. The static method suite( ) creates and returns the TestSuite .

Example 6-3. The class LibraryTests
 LibraryTests.java import junit.framework.*; public class  LibraryTests  extends TestSuite {    public static Test  suite  ( ) {       TestSuite suite = new TestSuite( );       suite.addTest(new TestSuite(  BookTest  .class));       suite.addTest(new TestSuite(  LibraryTest  .class));       suite.addTest(new TestSuite(  LibraryDBTest  .class));       suite.addTest(new TestSuite(  LibraryPerfTest  .class));       return suite;    } } 

When LibraryTests is run using TextTestRunner , all of the test methods in each test class are found and run, as shown in Example 6-4.

Example 6-4. Running the LibraryTests test suite
 $ java junit.textui.TestRunner LibraryTests ................. Time: 0.851 OK (17 tests) 

The GUI TestRunner can be used instead of TextTestRunner . The Swing version of TestRunner is invoked for LibraryTest as shown:

 $ java junit.swingui.TestRunner LibraryTest 

Figure 6-3 shows the result after the TestRunner GUI runs LibraryTest .

Figure 6-3. The Swing TestRunner after LibraryTest is run

Unit Test Frameworks
Unit Test Frameworks
ISBN: 0596006896
EAN: 2147483647
Year: 2006
Pages: 146
Authors: Paul Hamill

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net