JUnit#39;s @TestMethodOrder annotation not working(JUnit的@TestMethodOrder批注不起作用)
本文介绍了JUnit的@TestMethodOrder批注不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在进行以下集成测试时遇到问题
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
@SpringBootTest
@ActiveProfiles("test")
@TestMethodOrder(OrderAnnotation.class)
public class FooServiceIT {
@Test
@Order(1)
void testUploadSuccess() { ... }
@Test
@Order(2)
void testDownloadSuccess() { ... }
@Test
@Order(3)
void testDeleteSuccess() { ... }
}
运行测试时,我预计执行顺序为1、2、3,但由于某种原因,实际执行顺序为2、3、1。
tbh,我不知道为什么注释不起作用。我使用的是带有JUnit5.4的Spring Boot 2.1.3。
推荐答案
您需要正确配置您的集成开发环境。
要求
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
请勿使用提供您的IDE的JUnit5。如果将其添加为库,您将获得:
No tests found for with test runner 'JUnit 5'
==================== and this exception ===================
TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.SecurityException: class "org.junit.jupiter.api.TestMethodOrder"'s signer information does not match signer information of other classes in the same package
因此,只需包含提到的依赖项,您的代码就会按预期运行:
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FooServiceIT {
@Test
@Order(1)
public void testUploadSuccess() {
System.out.println("1");
}
@Test
@Order(2)
public void testDownloadSuccess() {
System.out.println("2");
}
@Test
@Order(3)
public void testDeleteSuccess() {
System.out.println("3");
}
}
JUnit结果:
1
2
3
这篇关于JUnit的@TestMethodOrder批注不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:JUnit的@TestMethodOrder批注不起作用


猜你喜欢
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01