JUnit4 简介

来源:互联网 发布:桌面的数据都没了 编辑:程序博客网 时间:2024/06/11 19:45

JUnit4是一个易学易用的Java单元测试框架,使用非常广泛。现阶段的最新版本号是4.12,JUnit5目前正在测试中,所以这里还是以JUnit4为准。

引入JUnit

现在主流的IDE比如IDEA或者Eclipse都提供了对JUnit4的支持,可以非常方便的使用JUnit4。当你在代码中添加了@Test注解,然后使用IDE的自动补全功能时,一般情况下IDE会弹出对话框询问你是否将JUnit4库添加到项目的类路径下。

当然也可以自己手动添加JUnit4的依赖。如果使用Maven,添加如下一段:

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.12</version>    <scope>test</scope></dependency>

如果使用Gradle,添加如下一段:

testCompile group: 'junit', name: 'junit', version: '4.12'

使用以上这些构建工具还有一个好处就是可以指定作用域,在上面我们将JUnit的作用域指定为测试时期,也就是说当我们实际发布代码的时候并不会包含JUnit的包。

基本使用

下面演示了JUnit的基本使用方法。

public class AppTest {    private User user;    @Before    public void init() {        user = new User();        user.setId(1);        user.setUsername("yitian");        user.setPassword("1234");        user.setBirthday(LocalDate.now());    }    @Test    public void testBean() {        Assert.assertNotNull(user);    }}

要让一个方法变成测试方法,只需要向其添加@Test注解即可。在测试方法中我们可以使用传统的System.out.println方法来输出,也可以使用各种日志框架来打印日志。还可以使用几个注解来初始化和清理测试方法用到的数据。Before和After注解会在每个测试方法之前和之后调用。BeforeClass和AfterClass注解会在所有测试方法之前和之后调用。这两个方法实际上是作为静态方法使用的,所以初始化的数据必须定义为静态的。由于名字上可能引起混淆,所以在JUnit5中后两个注解重新命名为BeforeEach和AfterEach。

public class AppTest {    private static Connection connection;    @BeforeClass    public static void init() throws SQLException {        connection = DriverManager.getConnection("jdbc:mysql///test");    }    @AfterClass    public static void clean() throws SQLException {        connection.close();    }    @Test    public void testBean() {        Assert.assertNotNull(connection);    }}

定义好测试方法之后,就可以测试了。在IDEA中,直接点击测试类旁边的绿色箭头即可运行。如果在Eclipse中,需要点击运行按钮,然后选择作为JUnit运行。

断言

除了在测试方法中使用输出语句之外,还可以使用JUnit提供的断言,来判断程序是否符合某个条件,如果断言为真,测试通过,如果断言为假,测试失败。断言在org.junit.Assert类中,有一组以assert开头的方法用于断言测试,基本上涵盖了大部分需求。下面列举几个常用的,如果有需要的话可以直接调用assertFail方法让断言直接失败。

断言方法 作用 assertTrue 真值断言 assertFalse 假植断言 assertEquals 相等断言 assertNotEquals 不等断言 assertNull 空值断言 assertNotNull 非空值断言 assertFail 断言失败

有了这些断言,就可以方便地测试了。我们可以创建一个对象,然后调用这些断言,将对象的实际状态和我们的预期结果进行比较,如果断言失败,我们就知道什么地方出现了问题。

使用Matchers

除了使用基本的断言,还可以使用Matchers进行更方便自然的测试。假如我们要测试一个字符串是否包含color或者colour。使用普通断言如下:

assertTrue(responseString.contains("color") || responseString.contains("colour"));// ==> failure message: // java.lang.AssertionError:

如果使用Matchers的话就像这样:

assertThat(responseString, anyOf(containsString("color"), containsString("colour")));// ==> failure message:// java.lang.AssertionError: // Expected: (a string containing "color" or a string containing "colour")// got: "Please choose a font"

可以看到,不论是语法还是测试输出,使用Matcher都更加易读。JUnit官方还列举了几个例子,从中我们就可以看到使用Matchers的方便之处。

assertThat(x, is(3));assertThat(x, is(not(4)));assertThat(responseString, either(containsString("color")).or(containsString("colour")));assertThat(myList, hasItem("3"));

要使用Matchers,需要使用org.junit.Assert类的assertThat方法,然后将要断言的对象和Matcher谓语参数传入。又细心的同学可能会发现如果使用Maven或者Gradle,添加了JUnit的话会同时包含另一个依赖项Hamcrest,这个包中就定义着大量谓语,可以让我们方便的进行测试。详细情况请参见Hamcrest的Java文档。

忽略测试

要忽略某个测试,只需要在测试方法上添加Ignore注解,还可以使用一个可选的字符串说明忽略测试的原因。

@Ignore("Test is ignored as a demonstration")@Testpublic void testSame() {    assertThat(1, is(1));}

测试的超时

针对可能耗费大量时间的测试,还可以为测试设定一个时间,如果超过该时间测试直接失败。

要为某一个测试方法设定超时,在Test注解时传入timeout参数,单位是毫秒:

@Test(timeout=1000)public void testWithTimeout() {  ...}

要为某个测试类中的所有方法设定超时,需要在测试类中添加一个org.junit.rules.Timeout的字段并用@Rule注解。

import org.junit.Rule;import org.junit.Test;import org.junit.rules.Timeout;public class HasGlobalTimeout {    public static String log;    private final CountDownLatch latch = new CountDownLatch(1);    @Rule    public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds max per method tested    @Test    public void testSleepForTooLong() throws Exception {        log += "ran1";        TimeUnit.SECONDS.sleep(100); // sleep for 100 seconds    }    @Test    public void testBlockForever() throws Exception {        log += "ran2";        latch.await(); // will block     }}

与其他框架的集成

这个特性得益于JUnit的运行器机制,它允许第三方软件创建运行器,以自己的方式运行JUnit测试。如果在一个普通项目中,我们可以使用IDE提供的运行测试功能来运行测试,IDE会为我们生成图形化的运行结果,用颜色来区分测试的成功与否。如果使用Mavne或Gradle,我们可以使用这些工具提供的测试命令来运行所有测试,生成测试结果。

Spring也提供了自己的运行器。如果在Spring项目中我们可以通过添加@RunWith注解并使用Spring运行器,这样测试类就会运行在Spring环境中,我们可以使用Spring的依赖注入将测试对象直接注入到测试类中。当然其他的Spring框架特性也支持。

@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests {    @Autowired    private User user;    @Test    public void testBean() {    }}

以上就是JUnit4 单元测试框架的一些简单使用。详细情况可以参见它的官方网站。另外JUnit5已经进入Milestone版本了,相信正式版也不远了。等到JUnit5正式版出来时,我在为大家介绍新版JUnit的使用方法。

参考资料

https://github.com/junit-team/junit4/wiki/Matchers-and-assertthat
https://github.com/junit-team/junit4/wiki/Ignoring-tests
https://github.com/junit-team/junit4/wiki/Timeout-for-tests

0 0
原创粉丝点击