Skip to content

Commit fd42305

Browse files
committed
✨ 添加 JUnit 示例
1 parent 845b0ae commit fd42305

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

  • codes/javalib/src/test/java/io/github/dunwu/javalib/test
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.github.dunwu.javalib.test;
2+
3+
import org.junit.After;
4+
import org.junit.AfterClass;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.BeforeClass;
8+
import org.junit.FixMethodOrder;
9+
import org.junit.Ignore;
10+
import org.junit.Test;
11+
import org.junit.runners.MethodSorters;
12+
13+
/**
14+
* JUnit 使用示例。 请注意各个方法的执行顺序。
15+
* @author Zhang Peng
16+
*/
17+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
18+
public class JUnitTest {
19+
/**
20+
* @BeforeClass 注解指出这是附着在静态方法必须执行一次并在类的所有测试之前。 一般用于共享配置方法(如连接到数据库)。
21+
*/
22+
@BeforeClass
23+
public static void beforeClass() {
24+
System.out.println("call @BeforeClass");
25+
}
26+
27+
/**
28+
* @Before 注解修饰的方法必须在类中的每个测试之前执行,以便执行测试某些必要的先决条件。
29+
*/
30+
@Before
31+
public void before() {
32+
System.out.println("call @Before");
33+
}
34+
35+
@Test
36+
public void testA() {
37+
System.out.println("call @Test testA");
38+
int sum = 1 + 2 + 3;
39+
Assert.assertEquals(6, sum);
40+
}
41+
42+
@Test
43+
public void testC() {
44+
System.out.println("call @Test testC");
45+
}
46+
47+
@Test
48+
public void testB() {
49+
System.out.println("call @Test testB");
50+
}
51+
52+
/**
53+
* @After 注解修饰的方法在执行每项测试后执行(如执行每一个测试后重置某些变量,删除临时变量等)
54+
*/
55+
@After
56+
public void after() {
57+
System.out.println("call @After");
58+
}
59+
60+
/**
61+
* 当需要执行所有的测试在JUnit测试用例类后执行,@AfterClass注解可以使用以清理建立方法,(从数据库如断开连接)。 注意:附有此批注(类似于BeforeClass)的方法必须定义为静态。
62+
*/
63+
@AfterClass
64+
public static void afterClass() {
65+
System.out.println("call @AfterClass");
66+
}
67+
68+
/**
69+
* 当想暂时禁用特定的测试执行可以使用忽略注释。每个被注解为@Ignore的方法将不被执行。
70+
*/
71+
@Ignore
72+
public void ignore() {
73+
System.out.println("call @Ignore");
74+
}
75+
}

0 commit comments

Comments
 (0)