JUnit高级用法之@RunWith
文章目录
@RunWith
关于@RunWith注解,官方文档是这么描述的:
When a class is annotated with
@RunWithor extends a class annotated with@RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.
JUnit用例都是在Runner(运行器)来执行的。通过它,可以为这个测试类指定一个特定的Runner。那么大多数时候我们都没有使用@RunWith这个注解,这是为什么呢?其实,JUnit中有一个默认的Runner,它的名字叫BlockJunit4ClassRunner,但这是在JUnit4.4之后才引入的,对于4.4之前版本的JUnit,它的名字叫Junit4ClassRunner。在新版本的源代码中已经添加了注释来说明这个问题:
/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
* removed in the next major release. Please use
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
*/
@Deprecated
public class JUnit4ClassRunner extends Runner implements Filterable, Sortable {
...
写过关于Spring项目的单元测试的同学可能见过这样的写法,就是用JUnit加载Spring的配置文件以完成Context的初始化,然后从Context中取出Bean并完成测试:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class UserManagerTest {
@Autowired
ApplicationContext ctx;
@Test
public void testAddUser() {
try {
UserManager userManager = ctx.getBean(UserManager.class);
userManager.addUser();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意这里使用了@RunWith注解,表明这个类中的测试用例需要使用SpringJUnit4ClassRunner类来执行。
@RunWith(Suite.class)
其作用是使用JUnit执行一个测试套件。Suite类是JUnit自带的,意为套件,顾名思义,就是一套东西。通过它,可以把多个相关的测试类看做一个测试套件一起测试。看个例子:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ TestA.class, TestB.class, /*Any test class you want to run*/})
public class TestSuite {
// Please note this case won't run. It will only run cases which
// are configured in @Suite.SuiteClasses
@Test
public void testPrint() {
System.out.println("Hello");
}
}
@RunWith指定了Suite类,说明这个TestSuite类是一个套件。通过@Suite.SuiteClasses指定了要执行的测试类(这些类中的所有用例都会执行)。
需要注意的是,这个TestSuite类本身用例则不会执行了(如上面的testPrint()方法)。
@RunWith(Parameterized.class)
Parameterized类也是JUnit自带的,用于使用多个参数组合多次执行同一个测试用例。看下面的例子:
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;
}
}
执行结果如下:

可以看到,虽然TestParameterized类中只有两个测试用例testAdd和testPrint,但是结果输出了8行,即每个用例都执行了4遍。
使用Parameterized注解需要注意几点:
该方法要有构造函数
有一个public static的方法被@Parameters标注,并且该方法只能返回Iterable类型或数组类型的数据(源代码是如下处理的)
if (parameters instanceof Iterable) { return (Iterable
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论