View RSS Feed

My Java Tips

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

Rate this Entry
by , 05-21-2011 at 11:28 PM (2313 Views)
This post presents an example that shows how to use @BeforeClass and @AfterClass annotation.

Java Code:
    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:

Java Code:
oneTimeSetUp()
setUp()
testEmptyCollection()
tearDown()
setUp()
testOneItemCollection()
tearDown()
oneTimeTearDown()

Submit "Sharing setUp() and tearDown() code for all tests - II" to Facebook Submit "Sharing setUp() and tearDown() code for all tests - II" to Digg Submit "Sharing setUp() and tearDown() code for all tests - II" to del.icio.us Submit "Sharing setUp() and tearDown() code for all tests - II" to StumbleUpon Submit "Sharing setUp() and tearDown() code for all tests - II" to Google

Tags: setup, teardown Add / Edit Tags
Categories
JUnit

Comments