Junit - 优先级测试(FixMethodOrder Test)

@FixMethodOrder的顺序也并不一定是方法在代码中定义的顺序,这与JVM的实现有关,我猜在class中方法名是保存在一个map中,不同JVM对map的实现不同,导致并不一定是按代码定义顺序的。

我们在写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),则这个执行顺序才是我想要的顺序。

这里写图片描述


  1. Junit - 测试框架介绍
  2. Junit - Eclipse 教程
  3. Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
  4. Junit - 断言方法(Assert Methods)
  5. Junit - 参数化测试(Parameterized Test)
  6. Junit - 套件测试(Suite Test)
  7. Junit - 忽略测试(Ignore Test)
  8. Junit - 超时测试(Timeout Test)
  9. Junit - 期望异常测试(Expected Test)
  10. Junit - 优先级测试(FixMethodOrder Test)
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
JUnit-BOM(Bill of Materials)是JUnit团队为JUnit 5框架提供的一个特殊模块。它的目的是简化JUnit 5的依赖管理。通过添加JUnit-BOM到你的项目中,你可以使用JUnit 5的所有核心模块和扩展模块,而无需单独指定每个模块的版本。 要使用JUnit-BOM,你需要在你的项目的构建文件中添加一个依赖项。对于Maven项目,可以在`<dependencies>`标签中添加以下内容: ``` <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.x.x</version> <!-- 替换为你想要使用的JUnit 5版本 --> <type>pom</type> <scope>import</scope> </dependency> ``` 对于Gradle项目,可以在`dependencies`块中添加以下内容: ``` dependencies { testImplementation platform('org.junit:junit-bom:5.x.x') // 替换为你想要使用的JUnit 5版本 testImplementation 'org.junit.jupiter:junit-jupiter-api' // 添加其他所需的JUnit 5模块 } ``` 添加JUnit-BOM之后,你可以使用JUnit 5的各种功能,例如编写和运行JUnit单元测试。你可以使用`@Test`注解来标记测试方法,并使用JUnit提供的各种断言和注解来编写强大的单元测试。 通过引用JUnit-BOM,你可以确保你的项目使用与JUnit 5兼容的版本,并且可以轻松管理依赖关系。这使得在不同的项目中使用JUnit 5变得更加简单和一致。 JUnit-BOM的详细信息可以在JUnit官方文档中找到。 JUnit是一个用于编写和运行可重复自动化测试的Java单元测试框架。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值