tp5网站开发逻辑架构,网站的特征,宣传网站制作,做套现网站建立 Spring Boot 单元测试方法一般依赖于 JUnit4 或 JUnit5 框架。
在高版本的 Spring Boot 中#xff0c;一般默认用的是 JUnit5。此时通过添加 SpringBootTest 注解#xff0c;即可成功注入相关的 bean 对象#xff0c;并进行测试。
import org.junit.jupiter.api.Test…建立 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 {Autowiredprivate LimitServiceImpl limitServiceImpl;Testpublic 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 {Autowiredprivate LimitServiceImpl limitServiceImpl;Testpublic void test() {}
}参考
何时使用RunWithSpring Boot 单元测试基本使用不污染数据库操作Spring Boot 单元测试通过 MockMvc 模拟 HTTP 请求