单元测试--Junit

Junit是Java的单元测试框架提供了一些注解方便我们进行单元测试

1. 常用注解

常用注解:

  • @Test
  • @BeforeAll,@AfterAll
  • @BeforeEach,@AfterEach

使用这些注解需要先引入依赖:

XML 复制代码
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

1.1 @Test

@Test用于方法,表示该方法是一个测试方法

添加上@Test注解可以看到左边出现了绿色三角形,点击可运行对应方法,点击类名左边的则是运行类中的所有测试方法。

1.2 @BeforeAll @AfterAll

@BeforeAll注释的方法会在该类中的所有测试方法执行前执行;

@AfterAll注释的方法会在该类中所有测试方法执行后执行;

被这两个注解注释的方法只能声明为静态方法

java 复制代码
public class JunitTest {
    @Test
    void test01() {
        System.out.println("test01");
    }

    @Test
    void test02() {
        System.out.println("test02");
    }

    @BeforeAll
    static void beforeAll() {
        System.out.println("BeforeAll");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("AfterAll");
    }
}

1.3 @BeforeEach @AfterEach

@BeforeEach注释的方法会在该类中的每个测试方法执行前都执行一次;

@AfterEach注释的方法会在该类中每个测试方法执行后都执行一次;

java 复制代码
public class JunitTest {
    @Test
    void test01() {
        System.out.println("test01");
    }

    @Test
    void test02() {
        System.out.println("test02");
    }

    @BeforeAll
    static void beforeAll() {
        System.out.println("BeforeAll");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("AfterAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("BeforeEach");
    }

    @AfterEach
    void afterEach() {
        System.out.println("AfterEach");
    }
}

2. 指定测试用例执行顺序

java 复制代码
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {
    @Order(2)
    @Test
    void test01() {
        System.out.println("test01");
    }

    @Order(1)
    @Test
    void test02() {
        System.out.println("test02");
    }
}

3. 参数化

我们不能直接给测试方法添加参数,需要通过注册的方式。

引入依赖:

XML 复制代码
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

3.1 单参数

java 复制代码
    //单参数
    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3, 4})
    void test03(int x) {
        System.out.println("test03, x = " + x);
    }

@ParameterizedTest 注解用于表示方法为一个参数测试方法,允许运行同一个测试方法多次,但使用不同的参数值来执行测试

@ValueSource用于为@ParameterizedTest 提供单一类型的参数值。

3.2 多参数

java 复制代码
    //多参数
    @ParameterizedTest
    @CsvSource({"'小明', '18'", "'小红', '19'", "'小华', '20'"})
    void test04(String name, int age) {
        System.out.println(name + "今年" + age + "岁");
    }
java 复制代码
    //多参数,文件传递
    @ParameterizedTest
    @CsvFileSource(resources = "test05.csv")
    void test05(String name, int age) {
        System.out.println(name + "今年" + age + "岁");
    }

3.3 通过方法生成参数

java 复制代码
    @ParameterizedTest
    @MethodSource("generate")
    void test06(String name, int age) {
        System.out.println(name + "今年" + age + "岁");
    }
    public static Stream<Arguments> generate() {
        return Stream.of(
                Arguments.arguments("A", 12),
                Arguments.arguments("B", 13),
                Arguments.arguments("C", 14),
                Arguments.arguments("D", 15)
        );
    }

arguments()方法接受的是一个可变参数,可以接收任意数量的参数

4. 测试套件

实际开发中会有多个类需要我们测试,所以我们也需要管理多个测试类的运行。

引入依赖:

XML 复制代码
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.9.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
java 复制代码
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({JunitTest.class, JunitTest2.class})
public class RunTest {
}

创建一个类使用入上注解

@SelectClasses表示通过class运行测试用例,这里先运行JunitTest再运行JunitTest2

也可以使用包运行:

java 复制代码
@Suite
//@SelectClasses({example.JunitTest.class, example.JunitTest2.class})
@SelectPackages(value = {"example"})
public class RunTest {
}

注意RunTest类不能在这个包中

相关推荐
梁辰兴1 天前
软件工程:单元测试
单元测试·软件工程·梁辰兴·‘设计原则·方法技术·tdd驱动·mock隔离
盟道科技1 天前
Redis+Lua 实现滑动窗口限流:从固定窗口踩坑到生产可用的完整方案
redis·junit·lua
2601_962299884 天前
python脚本如何单元测试
python·单元测试·pytest·unittest·mock对象
牛油果子哥q5 天前
C++大型项目工程精讲:CMake完整实战、静态库&动态库、模块化拆分、单元测试、gdb调试、性能工具、工程踩坑全解
开发语言·c++·单元测试
leoZ2316 天前
AI+前端提效-09 AI赋能前端测试:单元测试、E2E测试自动生成,提升覆盖率
前端·人工智能·opencv·目标检测·数据挖掘·单元测试·语音识别
呆呆敲代码的小Y8 天前
Lua 上值(UpValue)
开发语言·junit·lua·lua上值·upvalue
呆呆敲代码的小Y8 天前
【游戏开发】xLua 性能优化技巧
junit·性能优化·lua·xlua
边吃番茄边敲代码10 天前
从本地工具到 MCP:给 Agent 接入独立工具服务,并补齐自动化测试
人工智能·python·功能测试·ai·单元测试·pytest
qq_4523962310 天前
第十三篇:《测试策略:单元测试、组件测试、E2E 测试的落地实践》
单元测试