Spring Boot 单元测试什么时候需要添加 @RunWith

建立 Spring Boot 单元测试方法一般依赖于 JUnit4 或 JUnit5 框架。

在高版本的 Spring Boot 中,一般默认用的是 JUnit5。此时通过添加 @SpringBootTest 注解,即可成功注入相关的 bean 对象,并进行测试。

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class testClass {
	@Autowired
	private LimitServiceImpl limitServiceImpl;

	@Test
	public void test() {}
}

如果使用的是 JUnit4,则需要额外添加 @Runwith(SpringRunner.class) 注解,用于声明测试的环境为 Spring 环境。

import org.junit.Test;
import org.junit.runner.Runwith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@Runwith(SpringRunner.class)
@SpringBootTest
class testClass {
	@Autowired
	private LimitServiceImpl limitServiceImpl;

	@Test
	public void test() {}
}

参考

相关推荐

  1. Spring Boot 单元测试什么时候需要添加 @RunWith

    2024-07-21 05:46:06       23 阅读
  2. 精通SpringBoot单元测试

    2024-07-21 05:46:06       40 阅读
  3. springboot 单元测试

    2024-07-21 05:46:06       37 阅读
  4. SpringBoot单元测试剖析

    2024-07-21 05:46:06       36 阅读
  5. SpringBoot单元测试

    2024-07-21 05:46:06       31 阅读
  6. SpringBoot单元测试

    2024-07-21 05:46:06       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 05:46:06       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 05:46:06       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 05:46:06       45 阅读
  4. Python语言-面向对象

    2024-07-21 05:46:06       55 阅读

热门阅读

  1. leetcode--链表类题目总结

    2024-07-21 05:46:06       15 阅读
  2. Python实现精确读取PDF文件的全部内容(8)

    2024-07-21 05:46:06       16 阅读
  3. Python模块化编程:import机制剖析

    2024-07-21 05:46:06       17 阅读
  4. setlocal enabledelayedexpansion 详解

    2024-07-21 05:46:06       20 阅读
  5. MySQL中EXPLAIN关键字详解

    2024-07-21 05:46:06       14 阅读
  6. MFC:只允许产生一个应用程序实例的具体实现

    2024-07-21 05:46:06       19 阅读
  7. LeetCode题(66,69,35,88)--《c++》

    2024-07-21 05:46:06       18 阅读
  8. 【极客日常】Golang一个的slice数据替换的bug排查

    2024-07-21 05:46:06       21 阅读
  9. Fabric:Fabric-Gateway-Go的使用方法

    2024-07-21 05:46:06       16 阅读
  10. 机器学习 - 信息增益

    2024-07-21 05:46:06       20 阅读
  11. lua 写一个 不同时区之间转换日期和时间 函数

    2024-07-21 05:46:06       19 阅读
  12. 探索Perl的文件系统插件:灵活的系统扩展

    2024-07-21 05:46:06       18 阅读