Java Reference
In-Depth Information
setUp
test
tearDown
Figure 12-1. JUnit lifecycle
As Figure 12-1 shows, JUnit executes those three methods in sequence for each method identified
with the @Test annotation until all the test methods in the test case have been executed. Listing 12-2
shows an example test case using all three of the discussed annotations.
Listing 12-2. Test of Foo
package com.apress.springbatch.chapter12;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class FooTest {
private Foo fooInstance;
@Before
public void setUp() {
fooInstance = new Foo();
}
@Test
public void testBar() {
String results = fooInstance.bar();
assertNotNull("Results were null", results);
assertEquals("The test was not a success", "success", results);
}
@After
public void tearDown() {
fooInstance.close();
}
}
Search WWH ::




Custom Search