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 小时前
pytest_自动化测试5
python·功能测试·单元测试·集成测试·pytest
软件测试君1 小时前
自动化测试路线图之自动化测试完整指南
自动化测试·软件测试·测试工具·面试·职场和发展·单元测试·职场经验
不做菜鸟的网工1 小时前
Python 会话数压测脚本
测试
紫丁香16 小时前
pytest_自动化测试3
开发语言·python·功能测试·单元测试·集成测试·pytest
兰.lan20 小时前
【黑马ai测试】Day01课堂笔记+课后作业
软件测试·笔记·python·ai·单元测试
紫丁香1 天前
pytest_自动化测试1
开发语言·python·功能测试·单元测试·pytest
紫丁香1 天前
pytest_自动化测试4
python·功能测试·单元测试·集成测试·pytest
念越1 天前
第十七届蓝桥杯软件测试模拟赛1期题目及解析
蓝桥杯·测试用例·测试
Lyyaoo.2 天前
Spring Boot日志
spring boot·缓存·单元测试
bug攻城狮2 天前
为什么 Spring Boot 要单元测试?
spring boot·后端·单元测试