Sharing setUp() and tearDown() code for all tests - II
10 October 2008This 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()
Related Posts:
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