Sharing setUp() and tearDown() code for all tests - II

10 October 2008

This post presents an example that shows how to use @BeforeClass and @AfterClass annotation.

    public class SimpleTest {
 
        private Collection collection;
 
        @BeforeClass
        public static void oneTimeSetUp() {
            // one-time initialization code        
        }
 
        @AfterClass
        public static void oneTimeTearDown() {
            // one-time cleanup code
        }
 
        @Before
        public void setUp() {
            collection = new ArrayList();
        }
 
        @After
        public void tearDown() {
            collection.clear();
        }
 
        @Test
        public void testEmptyCollection() {
            assertTrue(collection.isEmpty());
        }
 
        @Test
        public void testOneItemCollection() {
            collection.add("itemA");
            assertEquals(1, collection.size());
        }
    }

Output:

oneTimeSetUp()
setUp()
testEmptyCollection()
tearDown()
setUp()
testOneItemCollection()
tearDown()
oneTimeTearDown()

del.icio.us:Sharing setUp() and tearDown() code for all tests - II  digg:Sharing setUp() and tearDown() code for all tests - II  spurl:Sharing setUp() and tearDown() code for all tests - II  wists:Sharing setUp() and tearDown() code for all tests - II  simpy:Sharing setUp() and tearDown() code for all tests - II  newsvine:Sharing setUp() and tearDown() code for all tests - II  blinklist:Sharing setUp() and tearDown() code for all tests - II  furl:Sharing setUp() and tearDown() code for all tests - II  reddit:Sharing setUp() and tearDown() code for all tests - II  fark:Sharing setUp() and tearDown() code for all tests - II  blogmarks:Sharing setUp() and tearDown() code for all tests - II  Y!:Sharing setUp() and tearDown() code for all tests - II  smarking:Sharing setUp() and tearDown() code for all tests - II  magnolia:Sharing setUp() and tearDown() code for all tests - II  segnalo:Sharing setUp() and tearDown() code for all tests - II  gifttagging:Sharing setUp() and tearDown() code for all tests - II

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Sharing setUp() and tearDown() code for all tests - II

Leave a Reply