
Junit 单元测试
JUnit4
常用注解
- @Test:声明为测试方法,方法要求为public void且throws Exception
- @BeforeClass/@AfterClass:在调用该类的测试方法前/后,统一执行一次
- @Before/@After:在调用每个测试方法前/后,都会执行一次
@RunWith(xxx.class):声明该测试类的运行器为xxx,未显式声明则使用默认
- Parameterized.class:参数运行器,配合@Parameters使用junit的参数化测试功能
- 把被测试函数需要使用的参数及其输出,作为类的变量,并设置相应的构造函数
- 使用@Parameters注解的方法(要求是public static Collection<Object[]>)来注入测试参数
- 每个Object[]就是一组数据,元素顺序保持与构造函数一致
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30@RunWith(Parameterized.class)
public class MyTest
{
private int val1;
private String val2;
private String expected;
private UserService userService = new UserServiceImpl();
public MyTest(int val1, String val2, String expected)
{
this.val1 = val1;
this.val2 = val2;
this.expected = expected;
}
@Parameterized.Parameters
public static Collection<Object[]> data()
{
List<Object[]> arr = new ArrayList<Object[]>();
arr.add(new Object[]{123,"345abc","wqwq"});
return arr;
}
@Test
public void test() throws Exception
{
String res = userService.getUserName(val1,val2);
assertEquals(expected, res);
}
}
- 每个Object[]就是一组数据,元素顺序保持与构造函数一致
- Suite.class:套件测试,用于一次运行多个测试类
- 定义一个空类,使用@Suite.SuiteClasses需要运行的测试类注入
1
2
3
4
5
6
7@RunWith(Suite.class)
@Suite.SuiteClasses({
Test1.class,
Test2.class
})
public class MyTest {
}
- 定义一个空类,使用@Suite.SuiteClasses需要运行的测试类注入
- Enclosed.class:嵌套测试,一个空的外部测试类,内部可以定义多个测试类
- 内部测试类必须是public static class
1
2
3
4
5
6
7
8
9
10
11
12
13
14@RunWith(Enclosed.class)
public class MyTest
{
public static class Test1{
@Test
public void test() throws Exception{
}
}
public static class Test2{
@Test
public void test() throws Exception{
}
}
}
- 内部测试类必须是public static class
JUnit 5 参数化测试核心注解
常用注解
- @BeforeAll/@AfterAll:在调用该类的测试方法前/后,统一执行一次
- @BeforeEach/@AfterEach:在调用每个测试方法前/后,都会执行一次
参数化测试:@ParameterizedTest
参数提供注解
@ValueSource: 提供单个参数(参数需是原始类型)
1
2
3
4@ParameterizedTest
@ValueSource(longs = {233,666})
public void test() throws Exception{
}@CsvSource: 提供多个参数(参数需是原始类型)
- 每组数据用一个字符串表示,字符串中的元素用逗号分隔,存在不提供的值,也需要隔开
1
2
3
4@ParameterizedTest
@CsvSource(value = {"arg1, 1 ,val1", "arg2,,val2"})
public void test(String arg, Integer value,String val) throws Exception{
}
- 每组数据用一个字符串表示,字符串中的元素用逗号分隔,存在不提供的值,也需要隔开
@CsvFileSource: 同上一个注解,但数据从csv文件读取:@CsvFileSource(resources = “/data.csv”)
@MethodSource: 从一个无参静态方法中获取
Stream
或Collection
作为多组参数(参数类型无限制)1
2
3
4
5
6
7
8
9
10
11
12@ParameterizedTest
@MethodSource("getParams")
public void test(User user, int time) throws Exception{
}
private static Stream<Arguments> getParams() {//无参静态方法提供参数
return Stream.of(
// 有效用户和时间
Arguments.of(new User("Alice"), 123),
Arguments.of(new User("Bob"), 456)
);
}