已解决
单元测试spring-boot-starter-test
来自网友在路上 146846提问 提问时间:2023-09-28 02:53:42阅读次数: 46
最佳答案 问答题库468位专家为你答疑解惑
参考博客: https://www.cnblogs.com/mzc1997/p/14306538.html
配置pom
junit-vintage-engine junit4
junit-jupiter-engine junit5
排除junit4使用junit5,两者在切换时要特别注意
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId>
</dependency>
命名规范
单元测试类的命名规范为:被测试类的类名+Test。.
单元测试类中测试方法的命名规范为:test+被测试方法的方法名+AAA,其中AAA为对同一个方法的不同的单元测试用例的自定义名称。
junit5的使用
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DisplayName("Base Test junit5才有") // 显示名称通常用于IDE和构建中的测试报告
class DemoFlowfestApplicationTests {@BeforeAllpublic static void beforall(){System.out.println("beforall***");}@Testvoid contextLoads() {System.out.println("run___1");}@Testvoid contextLoads2() {System.out.println("run___2");}@AfterAllpublic static void afterall(){System.out.println("after****");}}
beforall***
run___2
run___1
after****
@ActiveProfiles(“test”)
需要配置resources
junit4
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
// 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
@SpringBootTest(value={"com.example.demoflowfest.DemoFlowfestApplication"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//这里一定要有public
public class DemoFlowfestApplicationTestsTwo {@Beforepublic void befer(){System.out.println("befer***");}@Testpublic void contextLoads() {System.out.println("run___aa");}@Testpublic void contextLoadsa() {System.out.println("run___bb");}@Afterpublic void after(){System.out.println("after**");}
}
返回值
befer***
run___bb
after**
befer***
run___aa
after**
查看全文
99%的人还看了
相似问题
- Kotlin学习——kt里的集合,Map的各种方法之String篇
- Office文件在线预览大全-Word文档在线预览的实现方法-OFD文档在线预览-WPS文件在线预览
- composer切换全局镜像源的方法
- Python通过selenium调用IE11浏览器报错解决方法
- 测试用例的设计方法(全):正交实验设计方法|功能图分析方法|场景设计方发
- Java8新特性 ----- Lambda表达式和方法引用/构造器引用详解
- C#中抽象类、抽象方法和接口暨内联临时变量的精彩表达
- ChatGLM2 大模型微调过程中遇到的一些坑及解决方法(更新中)
- 类方法,静态方法和实例方法的区别及应用场景
- 【链表的说明、方法---顺序表与链表的区别】
猜你感兴趣
版权申明
本文"单元测试spring-boot-starter-test":http://eshow365.cn/6-14977-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!