如何做好UnitTest(一):junit4的使用及常用注解

399 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第12天,点击查看活动详情

单元测试在程序员的研发过程中起着越来越重要的作用。尤其是对于apache的开源项目,单元测试就更加重要了。基本上我们在向apache提交PR的过程中,每次提交的代码,还需要提交对应的UT case。否则不会被合并。此外还会为了提升UT的代码覆盖率,对UT的代码进行优化。本文介绍如何使用junt4,以及其基本的标签。为什么会选择junit4开始呢,因为junit是研发人员最熟悉的一个UT框架。而junit5又是一个跨度比较大的版本。部分工时也会用testNG,这都会在后续的文章中逐步来介绍。

1.junit4环境准备

在maven的项目中,使用junit是非常容易的,只需要在pom文件中做如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.dhb</groupId>
    <artifactId>junit-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <junit.version>4.13.2</junit.version>
    </properties>

    <dependencies>
        <!-- test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

这样idea就可以很方便的使用junit4了。

2.常用标签

现在对Junit中的主要标签进行介绍。

2.1 @Test

@Test是junit4中最基本的一个标签,用来标识该方法是否是测试用例。该方法要求必须是public void的方法。

public class JunitDemo {

    @Test
    public void testCase() {
        System.out.println("This is test case!");
    }
}

这样在idea里面就会被识别为测试用例,点右键就会有Run test case的小图标。 image.png 点击Run testcase,就会有如下输出:

image.png

2.2 @Ignore

@Ignore 标签是在junit4中用来忽略测试用例的标签,添加该标签之后,对应的测试用例不会执行。

@Ignore
public void ignoreCase() {
    System.out.println("This is ignore case!");
}

上述代码在测试用例执行的过程中不会被执行。会被跳过。

2.3 @BeforeClass 与 @AfterClass

@BeforeClass 与 @AfterClass,这两个注解是针对所有测试的,在用例执行的过程中只会执行一次,且必须为static void方法。

@BeforeClass
public static void before() {
    System.out.println("This is before!");
}

@AfterClass
public static void after() {
    System.out.println("This is after!");
}

关于执行顺序会在后面与@Before @After一起介绍。

2.4 @Before 与 @After

@Before:初始化方法,在测试用例执行之前执行。对于每一个测试方法都要执行一次。 @After:释放资源的方法,在测试用例执行之后执行。对于每一个测试方法都要执行一次。 需要注意的是与@BeforeClass 与 @AfterClass的区别。用如下代码演示:

public class JunitDemo {

    @BeforeClass
    public static void beforeClass() {
        System.out.println("***********This is beforeClass! ");
    }

    @Before
    public void before() {
        System.out.println("############This is before!");
    }
    @Test
    public void testCase() {
        System.out.println("This is test case!");
    }

    @Test
    public void testCase1() {
        System.out.println("This is test case1!");
    }

    @Ignore
    public void ignoreCase() {
        System.out.println("This is ignore case!");
    }

    @After
    public void after() {
        System.out.println("##########This is after!");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("***********This is afterClass!");
    }
}

上述代码执行结果如下:

***********This is beforeClass! 
############This is before!
This is test case1!
##########This is after!
############This is before!
This is test case!
##########This is after!
***********This is afterClass!

image.png 可以看出,beforeClass 和afterClass方法只执行了一次。而before和after则每个测试用例都会执行一次。