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

相关推荐
JudithHuang20 小时前
Vue3 Vitest 单元测试进阶
前端·单元测试·log4j·vue
JAVA面经实录91721 小时前
AI 辅助写单元测试
java·人工智能·单元测试·ai编程
泛联新安1 天前
Omni Unittest单元测试智能体,AI时代的软件质量门禁
单元测试·汽车软件·比亚迪
钱栈up1 天前
烧结趋势页台车速度曲线凭空消失:一次\\\"去重\\\"误杀的配置修复
后端·测试
慧都产品智能化2 天前
汽车软件年底自查:按安全影响排定缺陷整改顺序
c语言·c++·测试工具·单元测试·汽车·代码规范
林壮Allen2 天前
自检脚本说「全绿」,凭什么信它:用两条负对照把橡皮图章揪出来
后端·测试
狂师2 天前
推荐一款手机自动化神器:phone-harness
前端·开源·测试
回眸&啤酒鸭2 天前
【回眸】AI 大模型驱动 Tessy 单元测试集成效果全景展示
网络·人工智能·单元测试
狂师3 天前
别只会用 Playwright、Appium了?这 8 款开源 AI 自动化工具实测推荐!
人工智能·开源·测试