Junit 实例精讲基础教程(一) 认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass

2023-05-04 20:22:07 浏览数 (1)

Junit官网:http://junit.org/junit4/

关于Junit,官文甚至不做过多解释:Junit只是一个用于单元测试的小框架,是基于xUnit架构的一个实现。

系列教程基于Maven。

1. 引入Junit依赖

代码语言:javascript复制
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

贴士:Junit捆绑了一个hamcrest-core的类库,查看依赖树可以看到:

代码语言:javascript复制
dependency:tree
...
[INFO] - junit:junit:jar:4.12:test
[INFO]    - org.hamcrest:hamcrest-core:jar:1.3:test

如果你要使用hamcrest-core的其他版本,可以过滤掉hamcrest-core:

代码语言:javascript复制
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- This will get hamcrest-core automatically 
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>  -->
        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        <version>1.4-atlassian-1</version>
    </dependency>

</dependencies>

2. Junit中的基本注解

Junit中集中基本注解,是必须掌握的。

  • @BeforeClass – 表示在类中的任意public static void方法执行之前执行
  • @AfterClass – 表示在类中的任意public static void方法执行之后执行
  • @Before – 表示在任意使用@Test注解标注的public void方法执行之前执行
  • @After – 表示在任意使用@Test注解标注的public void方法执行之后执行
  • @Test – 使用该注解标注的public void方法会表示为一个测试方法

使用示例: BasicAnnotationTest.java:

代码语言:javascript复制
package org.byron4j.spring_mvc_log4j;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class BasicAnnotationTest {
    // Run once, e.g. Database connection, connection pool
    @BeforeClass
    public static void runOnceBeforeClass() {
        System.out.println("@BeforeClass - runOnceBeforeClass");
    }

    // Run once, e.g close connection, cleanup
    @AfterClass
    public static void runOnceAfterClass() {
        System.out.println("@AfterClass - runOnceAfterClass");
    }

    // Should rename to @BeforeTestMethod
    // e.g. Creating an similar object and share for all @Test
    @Before
    public void runBeforeTestMethod() {
        System.out.println("@Before - runBeforeTestMethod");
    }

    // Should rename to @AfterTestMethod
    @After
    public void runAfterTestMethod() {
        System.out.println("@After - runAfterTestMethod");
    }

    @Test
    public void test_method_1() {
        System.out.println("@Test - test_method_1");
    }

    @Test
    public void test_method_2() {
        System.out.println("@Test - test_method_2");
    }
}

输出:

代码语言:javascript复制
@BeforeClass - runOnceBeforeClass
@Before - runBeforeTestMethod
@Test - test_method_1
@After - runAfterTestMethod
@Before - runBeforeTestMethod
@Test - test_method_2
@After - runAfterTestMethod
@AfterClass - runOnceAfterClass

0 人点赞