当前位置: 首页 > news >正文

java单元测试:JUnit测试运行器

JUnit测试运行器(Test Runner)决定了JUnit如何执行测试。JUnit有多个测试运行器,每个运行器都有特定的功能和用途。

1. 默认运行器

当没有显式指定运行器时,JUnit会使用默认运行器,这在JUnit 4和JUnit 5之间有所不同。

JUnit 4 默认运行器

在JUnit 4中,默认运行器是BlockJUnit4ClassRunner

import org.junit.Test;
import static org.junit.Assert.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
JUnit 5 默认运行器

在JUnit 5中,不需要显式指定运行器,JUnit Jupiter会自动运行测试。

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}

2. @RunWith 注解

@RunWith注解用于指定JUnit 4的测试运行器。以下是一些常用的运行器:

2.1 SpringRunner

SpringRunner(原名SpringJUnit4ClassRunner)用于在JUnit 4中运行Spring测试,提供Spring应用程序上下文的支持。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class)
public class SpringRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
2.2 Parameterized

Parameterized运行器用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;@RunWith(Parameterized.class)
public class ParameterizedTest {private int input1;private int input2;private int expected;public ParameterizedTest(int input1, int input2, int expected) {this.input1 = input1;this.input2 = input2;this.expected = expected;}@Parameterized.Parameterspublic static Collection<Object[]> data() {return Arrays.asList(new Object[][] {{ 1, 2, 3 },{ 2, 3, 5 },{ 3, 5, 8 }});}@Testpublic void testAdd() {assertEquals(expected, input1 + input2);}
}
2.3 Suite

Suite运行器用于运行一组测试类。

import org.junit.runner.RunWith;
import org.junit.runners.Suite;@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class,TestClass2.class
})
public class TestSuite {// 空类,仅用于运行指定的测试类
}

3. JUnit 5 扩展模型

在JUnit 5中,不使用@RunWith,而是使用@ExtendWith注解来扩展测试功能。

3.1 SpringExtension

SpringExtension用于在JUnit 5中运行Spring测试,提供Spring应用程序上下文的支持。

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
public class SpringExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
3.2 ParameterizedTest

@ParameterizedTest注解用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;public class ParameterizedTestExample {@ParameterizedTest@CsvSource({"1, 2, 3","2, 3, 5","3, 5, 8"})void testAdd(int input1, int input2, int expected) {assertEquals(expected, input1 + input2);}
}

4. 自定义运行器和扩展

4.1 自定义JUnit 4 运行器

可以创建自定义运行器来扩展JUnit 4的功能。

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;public class CustomRunner extends BlockJUnit4ClassRunner {public CustomRunner(Class<?> klass) throws InitializationError {super(klass);}@Overrideprotected void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier) {System.out.println("Running test: " + method.getName());super.runChild(method, notifier);}
}import org.junit.Test;
import org.junit.runner.RunWith;@RunWith(CustomRunner.class)
public class CustomRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
4.2 自定义JUnit 5 扩展

可以创建自定义扩展来扩展JUnit 5的功能。

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;public class CustomExtension implements BeforeEachCallback {@Overridepublic void beforeEach(ExtensionContext context) {System.out.println("Before each test: " + context.getDisplayName());}
}import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;@ExtendWith(CustomExtension.class)
public class CustomExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}

5. 使用示例:Spring Boot 测试

结合使用@ExtendWith@SpringBootTest进行Spring Boot应用程序的集成测试。

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MyApplicationTests {@Autowiredprivate MyService myService;@Testpublic void testAdd() {assertEquals(5, myService.add(2, 3));}
}

总结

  • JUnit 4中,@RunWith用于指定测试运行器,常用的运行器包括SpringRunnerParameterizedSuite等。
  • JUnit 5中,@ExtendWith用于扩展测试功能,常用的扩展包括SpringExtensionParameterizedTest等。
  • 可以创建自定义运行器(JUnit 4)或扩展(JUnit 5)来满足特定测试需求。
  • @SpringBootTest用于Spring Boot应用程序的集成测试。

这些工具和技术使得JUnit能够灵活地适应各种测试需求。

http://www.lryc.cn/news/353083.html

相关文章:

  • 网络模型—BIO、NIO、IO多路复用、信号驱动IO、异步IO
  • 智能语义识别电影机器人的rasa实现
  • C# 实现腾讯云 IM 常用 REST API 之会话管理
  • MySQL之Schema与数据类型优化(三)
  • 大语言模型发展历史
  • Nginx - 安全基线配置与操作指南
  • 简述js的事件循环以及宏任务和微任务
  • [力扣题解] 797. 所有可能的路径
  • 【QT八股文】系列之篇章3 | QT的多线程以及QThread与QObject
  • 基于python flask的web服务
  • HTTP 响应分割漏洞
  • Algoriddim djay Pro Ai for Mac:AI引领,混音新篇章
  • 常见算法(3)
  • 集中抄表电表是什么?
  • 第八届能源、环境与材料科学国际学术会议(EEMS 2024)
  • 09.自注意力机制
  • 时政|杂粮产业
  • docker 安装 私有云盘 nextcloud
  • 第十一届蓝桥杯物联网试题(国赛)
  • 算法金 | Dask,一个超强的 python 库
  • Java 说唱歌手
  • 面试-软件工程与设计模式相关,Spring简介
  • IDEA中一些常见操作【持续更新】
  • java继承使用细节二
  • c++11 标准模板(STL)本地化库 - 平面类别(std::numpunct_byname) 表示系统提供的具名本地环境的 std::numpunct
  • XILINX FPGA DDR 学习笔记(一)
  • vue源码2
  • Android四大组件 Broadcast广播机制
  • redisson 使用fastJson2序列化
  • Python数据分析常用函数