test-02-java 单元测试框架 junit5 入门介绍

拓展阅读

junit5 系列

基于 junit5 实现 junitperf 源码分析

Auto generate mock data for java test.(便于 Java 测试自动生成对象信息)

Junit performance rely on junit5 and jdk8+.(java 性能测试框架。性能测试。压测。测试报告生成。)

junit5 的入门例子

maven 引入

xml 复制代码
<dependencies>
    <!-- 添加 JUnit 5 依赖项 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

方法

java 复制代码
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

测试类

java 复制代码
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    void testAdd() {
        // Arrange
        Calculator calculator = new Calculator();

        // Act
        int result = calculator.add(3, 7);

        // Assert
        assertEquals(10, result, "3 + 7 should equal 10");
    }
}

小结

junit5 对比 junit4 可以说是划时代的提升。

这么多年过去了,就算是再顽固的系统,现在也开始使用 jdk1.8 了,这一点令人欣慰。

junit5 是新时代的单元测试,所以忘掉你的 junit4 吧。

相关推荐
我要学脑机1 小时前
软件测试体系笔记:从模型到落地
测试
一孤程1 天前
游戏测试专题第七篇:游戏自动化测试实战
游戏·测试
2302_805771071 天前
Day13Jmeter数据驱动
功能测试·单元测试·测试用例·需求分析·测试
2302_805771071 天前
Day14Jmeter+数据库的操作
功能测试·单元测试·测试用例·需求分析
清风~徐~来1 天前
【软件测试】allure 测试报告模块
单元测试
ClouGence1 天前
Chrome Recorder 能用于长期回归测试吗?
前端·chrome·测试
一孤程1 天前
游戏测试专题第八篇:游戏安全测试与反外挂
游戏·测试
ClouGence1 天前
开发提效:CueCast MCP 构建自动化测试 Agent 工作流
agent·测试·mcp
2302_805771072 天前
Day18在接口自动化测试中引入pytest用例管理框架
功能测试·单元测试·测试用例·需求分析·测试