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 吧。

相关推荐
狂师9 小时前
爆火!新一代e2e AI测试框架,单日狂揽1500 star
前端·人工智能·测试
孟陬9 小时前
不用盲调性能参数 Vitest 5.0 新推出 `vitest doctor` 一键诊断测试慢卡点,自动给出最优提速配置
性能优化·单元测试·vite
冬奇Lab21 小时前
LLM 驱动的自动化测试系列(10):自愈定位——当启发式打分撞上语义理解的天花板
人工智能·测试
狂师1 天前
2026年APP自动化测试工具大全,12款主流工具一次看懂
前端·开源·测试
冬奇Lab2 天前
LLM 驱动的自动化测试系列(09):移动端自动化(五)——当通用 Agent 框架下沉到测试场景
人工智能·agent·测试
andyweike2 天前
iOS App 测试
ios·测试·ad hoc·分发
匠测AI说2 天前
AI for Testing 提效实战·执行自动化(一):别让AI凭空写脚本,让它在你的框架里写,产出才能直接合入
人工智能·测试
Zelman2 天前
测试层级与测试类型
后端·面试·测试
Ikalus19882 天前
预注册的答案表把正控判成「通过」,盲测判成「不通过」——而两个都不对
测试
小元同学2 天前
一个大三测试实习生的自白:实习重不重要?我在往测开的路上找到了答案
测试