public class JUnitFourVersionTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("tearDownAfterClass()");
}
@Before
public void setUp() throws Exception {
System.out.println("setUp()");
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown()");
}
@Test
public void helloOne() {
System.out.println("helloOne()");
Assert.assertEquals(2, 2);
}
@Test
public void helloTwo() {
System.out.println("helloTwo()");
}
public void helloThree() {
System.out.println("helloThree()");
}
@Test
@Ignore
public void helloFour() {
System.out.println("helloFour()");
}
@Test(timeout = 1000)
public void squareRoot() {
for (;;)
;
}
@Test(expected = IllegalArgumentException.class)
public void divideByZero() {
Calculation calculation = new Calculation();
calculation.div(5, 0);
}
}
本文介绍了 JUnit4 的新特性及其用法。通过示例代码展示了如何使用 @BeforeClass 和 @AfterClass 进行类级别的初始化和清理工作,@Before 和 @After 对每个测试方法的准备和清理,以及 @Test 注解的多种使用方式,包括忽略测试、设置超时时间和预期异常。
1838

被折叠的 条评论
为什么被折叠?



