尝试使用JDK 5.0进行开发,当然不能少了JUnit这个framework了。尽管在pre-release的时候我已经开始使用JUnit 4.0,真正用的时候还是翻了翻一下文档http://www.junit.org/index.htm,自己写了一个小例子。
JUnit通过Annotation的方式,让自己写完的Test Case看起来干净,简单,少了很多杂质似的。
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import junit.framework.JUnit4TestAdapter;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
/**
* @author Steven
*
*/
public class JUnit4ExampleTest {
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(JUnit4ExampleTest.class);
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testCase() {
assertTrue(true);
}
@Test(expected = RuntimeException.class)
public void testException() {
throwException();
fail("after exception");
}
@Test
@Ignore("Ignore flag!")
public void testIgnore() {
fail("The case should be ignored.");
}
private void throwException() {
throw new RuntimeException("Random");
}
}
注:Maven2的surefire plugin对于JUnit4没有进行支持或者说仍然不兼容,这点是需要注意的。