spring boot Junit 执行顺序
来源:互联网 发布:linux基线配置 编辑:程序博客网 时间:2024/05/17 02:54
我们在写JUnit测试用例时,有时候需要按照定义顺序执行我们的单元测试方法,比如如在测试数据库相关的用例时候要按照测试插入、查询、删除的顺序测试。如果不按照这个顺序测试可能会出现问题,比如删除方法在前面执行,后面的方法就都不能通过测试,因为数据已经被清空了。而JUnit测试时默认的顺序是随机的。所以这时就需要有办法要求JUnit在执行测试方法时按照我们指定的顺序来执行。
JUnit是通过@FixMethodOrder
注解(annotation)来控制测试方法的执行顺序的。@FixMethodOrder
注解的参数是org.junit.runners.MethodSorters
对象,在枚举类org.junit.runners.MethodSorters
中定义了如下三种顺序类型:
- MethodSorters.JVM
Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run (按照JVM得到的方法顺序,也就是代码中定义的方法顺序)
- MethodSorters.DEFAULT(默认的顺序)
Sorts the test methods in a deterministic, but not predictable, order() (以确定但不可预期的顺序执行)
- MethodSorters.NAME_ASCENDING
Sorts the test methods by the method name, in lexicographic order, with Method.toString() used as a tiebreaker (按方法名字母顺序执行)
举例说明
以下的代码,定义了三个方法testAddAndGet,testSearch,testRemove
,我设计的时候,是希望三个方法按定义的顺序来执行。
package test;import org.junit.Assert;import org.junit.FixMethodOrder;import org.junit.runners.MethodSorters;import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@FixMethodOrder(MethodSorters.JVM)//指定测试方法按定义的顺序执行 public class TestJNI { private static final Logger logger = LoggerFactory.getLogger(TestJNI.class); @Test public void testAddAndGet(){ logger.info("test 'addBean' and 'getBean' "); } @Test public final void testSearch() { logger.info("test search CODE from JNI memory..."); } @Test public final void testRemove() { logger.info("test remove CODE from JNI memory..."); } }
如果@FixMethodOrder
定义为MethodSorters.DEFAULT
或去掉代码中的@FixMethodOrder注解,那么测试用便执行的顺序是
这并不是我要的结果,testRemove
如果先执行了,testSearch
肯定什么也找不到。
如果改成@FixMethodOrder(MethodSorters.JVM)
,则这个执行顺序才是我想要的顺序。
- spring boot Junit 执行顺序
- junit执行顺序
- JUnit 执行顺序
- Junit 测试执行顺序
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring boot配置Junit
- Spring Boot Junit单元测试
- spring boot Junit单元测试
- spring-boot junit 单元测试
- spring boot使用 aop 处理方法的执行顺序
- Junit指定测试执行顺序
- Junit测试方法执行顺序
- JUnit 标签执行顺序解析
- Junit测试用例按顺序执行
- 删除字符串中重复的字符
- Java性能优化全攻略
- 机器学习中的范数规则化:L0、L1与L2范数
- 基础GPIO电路图(一)
- 如何对一个表表进行新加字段
- spring boot Junit 执行顺序
- 计算字符串和文件的MD5值
- DBUS与QDBUS的应用
- opnet调用matlab引擎
- java字符串中Emoji表情处理
- Spring Boot中的事务管理
- OpenWrt自定义按键功能
- 扛得住mysql 之 影响mysql的性能
- Redis安装