java - 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,我不知道为什么注释不起作用。我正在使用带有 JUnit 5.4 的 Spring Boot 2.1.3。
解决方案
您需要正确配置 IDE。
要求
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
不要使用提供 IDE 的JUnit 5 。如果将其添加为库,您将获得:
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
推荐阅读
- python - django.core.exceptions.FieldError:为用户指定的未知字段(电话)
- html - 我可以链接到不同页面上的特定图像吗?
- ruby-on-rails - Rails 生成设计安装问题(LoadError 文件)
- java - Spring boot - “一对多”属性值类型不应为错误
- javascript - 打字稿:数组中的自定义groupBy
- python - Django -JSON 在集成贝宝时无法序列化类型错误
- nginx - 通过 GKE 上的 NGINX Ingress 启用安全的 websocket 连接
- react-native - 如何使用 NativeDriver 在 onScroll 中设置多个 Animated.Values?
- ruby - 无法在 macOS 中安装 cocoapods
- c# - 如何批处理 IAsyncEnumerable
,在连续批次之间执行最大间隔策略?