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 天前
GoogTest测试框架
c++·测试
AmyLin_20011 天前
AI 如何做出数学发现【5】- AI 数学证明怎样做单元测试:从“看起来顺”到可否证工作流
人工智能·单元测试·openai·数学推理·符号计算·ai推理·反例生成
是小艺吗。1 天前
第三方机构出具的软件功能测试报告的作用与流程说明,中承信安专业讲解
功能测试·单元测试·软件测试报告·软件功能测试·软件测评
测试老哥2 天前
软件测试:单元测试详解
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
回眸&啤酒鸭3 天前
【回眸】搞钱灵感——智能体 汽车软件单元测试与集成测试
单元测试·汽车·集成测试
狂师4 天前
AI 测试提效 | 别搞万能 Skill,推荐用 5 个 Agent Skill 串起 UI 自动化全流程
前端·人工智能·测试
ClouGence5 天前
AI Agent 能写测试、跑流程,为什么回归测试还不能完全交给 AI?
前端·测试
小龙飞刀5 天前
Chrome插件自动化测试:单元测试、集成测试
chrome·单元测试·集成测试
circuitsosk5 天前
平台整体能力与功能特性设计:分群、联运、ABTest与运营位系统
python·机器学习·搜索引擎·ab测试·vllm·rag检索