Junit4 常用注解

Junit4 常用注解

一、Junit4常用注解

@Test

    Test注解应用于方法上,标注一个方法是可以进行运行测试,其方法必须是public void 类型!

@Ignore

    应用于方法或者类上面,可以用来忽略Junit运行的测试。

@BeforeClass

    在每一个测试类之前执行,标注于方法之上,一般用于初始化数据。

@AfterClass

    在每一个测试类之后执行,标注与方法之上,一般用于清理测试缓存以及关闭相关连接。

@Before

    标注于方法上,表示在每一个测试用例(@Test)注解的方法前都会进行执行。

@After

    标注于方法上,表示在每一个测试用例(@Test)注解的方法后都会进行执行。

@RunWith

JUnit高级用法之@RunWith

    注解于测试类上,用来确定这个测试类是如何进行运行

  1. @RunWith(Parameterized.class)注解于类上,之后使用@Parameters注解,注解于方法上传入参数,进行运行

@Parameters注解用于使用多个参数组合多次执行同一个测试用例。例子:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class TestParameterized {
//你的成员参数
private int expected;
private int first;
private int second;

//构造方法
public TestParameterized(int expected, int firstNumber, int secondNumber) {
this.expected = expected;
this.first = firstNumber;
this.second = secondNumber;
}

/**
* Note: @Parameters annotated method must be public static,注解方法必须静态公开,这里相当于初始化成员变量
* otherwise an Exception will thrown.
*/
@Parameters
public static List<Integer[]> parameters() {
return Arrays.asList(new Integer[][]{{3, 1, 2}, {5, 2, 3}, {7, 3, 4}, {9, 4, 5}});
}

@Test
public void testAdd() {
String format = "Using parameters: expect=%d, first=%d, second=%d";
System.out.println(String.format(format, expected, first, second));

Feature feature = new Feature();
assertEquals(expected, feature.add(first, second));
}

@Test
public void testPrint() {
String format = "Print ----------: expect=%d, first=%d, second=%d";
System.out.println(String.format(format, expected, first, second));
}
}

class Feature {
public int add(int i1, int i2) {
return i1 + i2;
}
}

执行结果:

image

使用@Parameterized注解需要注意几点:

  • 该方法要有构造函数有一个public static的方法被@Parameters标注,并且该方法只能返回Iterable类型或数组类型的数据(源代码是如下处理的)
Text
1
2
3
4
5
6
7
if (parameters instanceof Iterable) {
return (Iterable<Object>) parameters;
} else if (parameters instanceof Object[]) {
return Arrays.asList((Object[]) parameters);
} else {
throw parametersMethodReturnedWrongType();
}

因为上面的方式使用了构造方法来初始化数据,其实也可以使用字段注入来代替构造方法,只需稍加改变TestParameterized类即可:

  1. 用Parameter参数来修饰属性。注意:索引从0开始
  2. 属性要用public修饰
Text
1
2
3
4
5
6
@Parameter(0)
public int expected;
@Parameter(1)
public int first;
@Parameter(2)
public int second;
  1. RunWith(Categories.class)注解于类上,可以使用更小的类别执行测试

各个测试用例上用@Category标注该用例属于那些“类别。例子:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*-----TestA.java-----*/
import org.junit.Test;
import org.junit.experimental.categories.Category;

class Feature1 {}
class Feature2 {}

public class TestA {
@Test
@Category(Feature1.class)
public void testAdd() {
System.out.println("A.testAdd");
}

@Test
@Category(Feature2.class)
public void testAdd2() {
System.out.println("A.testAdd2");
}

@Test
@Category({Feature1.class, Feature2.class})
public void testAdd3() {
System.out.println("A.testAdd3");
}
}

/*-----TestCategory.java-----*/
import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.ExcludeCategory;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Categories.class)
@IncludeCategory(Feature1.class)
@ExcludeCategory(Feature2.class)
@Suite.SuiteClasses({
TestA.class
/*Any test class you want to run*/
})
public class TestCategory {
// Do nothing
}

    其中,Feature1和Feature2代表两个不同的“类型”,TestA类中通过@Category标注了各个用例(可以为一个用例指定多个Category,例如上方的testAdd3方法)。@IncludeCategory指明了需要执行的类型,而@ExcludeCategory指明了不希望执行的类型。

  1. @RunWIth(SpringJUnit4ClassRunner)

SpringJUnit4ClassRunner is a custom extension of JUnit’s BlockJUnit4ClassRunner which provides functionality of the Spring TestContext Framework to standard JUnit tests by means of the TestContextManager and associated support classes and annotations.

To use this class, simply annotate a JUnit 4 based test class with @RunWith(SpringJUnit4ClassRunner.class) or @RunWith(SpringRunner.class).

The following list constitutes all annotations currently supported directly or indirectly by SpringJUnit4ClassRunner. (Note that additional annotations may be supported by various TestExecutionListener or TestContextBootstrapper implementations.)

SpringJunit4ClassRunner实际上继承了BlockJUnit4ClassRunner(Junit4默认启动器) 在继承基本的Junit4测试方法的支持的情况下,增加了完整的SpringFrameWork框架支持,使得可以运行Spring相关的框架功能。

  1. @RunWith(Suit.class) 和 @SuitClasses()注解

    使用测试套件进行测试,其@SuitClasses()注解于类上,基本使用如下:

1
2
3
4
5
6
7
8
9
//使用套件测试
@RunWith(Suite.class)
//括号中填写你需要执行的类
@Suite.SuiteClasses({
AdminMapperTest.class,
SortTypeMapperTest.class
})
public class RepositorySuitTest {
}

二、Spring结合使用注解

@Transational(和Spring事务相同)

单元测试开启事务设置:配置TransactionalTestExecutionListener

    为了使单元测试使用事务,需要配置TransactionalTestExecutionListener类,可以通过继承AbstractTransactionalJUnit4SpringContextTests类,或直接使用TransactionalTestExecutionListener类的方式实现:

  1. 继承AbstractTransactionalJUnit4SpringContextTests类

    单元测试类可直接或间接继承自AbstractTransactionalJUnit4SpringContextTests类,在AbstractTransactionalJUnit4SpringContextTests类中通过@TestExecutionListeners注解指定了TransactionalTestExecutionListener类,在类级别指定了@Transactional注解。

  1. 直接使用TransactionalTestExecutionListener类

    在单元测试类或超类中可以通过@TestExecutionListeners注解直接指定TransactionalTestExecutionListener类,并在测试类(或超类)或测试方法级别声明@Transactional注解。

示例如下:

1
2
@TestExecutionListeners({TransactionalTestExecutionListener.class})
@Transactional

@Transactional注解可以在测试类级别或测试方法级别指定,

@Rollback

  1. 指定使用了事务的测试方法在完成时是否应当回滚。当@Rollback注解value值为true时,事务会回滚,否则会提交。@Rollback注解value值默认为true。
  2. 可在测试类级别或测试方法级别指定,若在测试类级别指定,则该测试类的所有测试方法会使用相同的@Rollback注解配置。

@Commit

  1. 指定使用了事务的测试方法在完成时应当提交.
  2. 声明在测试类级别或测试方法级别。

@RunWith

见上文