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

相关推荐
阿狸猿2 小时前
单元测试中静态测试、动态测试及白盒测试、回归测试实践
单元测试·软考
Max_uuc2 小时前
【工程心法】从“在板盲调”到“云端验证”:嵌入式单元测试与 TDD 的工程化革命
单元测试·tdd
feathered-feathered1 天前
测试实战【用例设计】自己写的项目+功能测试(1)
java·服务器·后端·功能测试·jmeter·单元测试·压力测试
测试渣1 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
minh_coo1 天前
Spring单元测试之反射利器:ReflectionTestUtils
java·后端·spring·单元测试·intellij-idea
科技块儿1 天前
【数据亲测】商业IP库在广告ab测试中的roi提升效果分析
网络·tcp/ip·ab测试
洞窝技术2 天前
让AI帮我做测试用例,我来喝咖啡
ai编程·测试
金銀銅鐵2 天前
浅解 JUnit 4 第九篇:JUnitCore (下)
junit·单元测试
A懿轩A2 天前
【Maven 构建工具】Maven + JUnit5 单元测试实战:测试级别、注解、断言与 Maven test 阶段
java·单元测试·maven