You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JUnit5 @Order注解被忽略 @TestMethodOrder配置后测试执行顺序随机问题

更新:我本应附上原始POM文件(见下方),另外我已经尝试过JUnit的@TestMethodOrder注解不生效相关问题的已有解决方案

需求

按照指定顺序执行JUnit测试

遇到的问题
  • 当前的顺序配置注解未生效或被忽略,代码未做任何修改的情况下,每次运行测试的执行顺序都随机变化。我后续还有约5个测试方法需要添加,测试执行顺序的重要性会进一步提升。
  • 我是参考JUnit 5官方用户指南的2.9章节内容进行配置的。
运行环境
  • JUnit 5.6.0
  • Java 16
  • 搭载Maven的Netbeans IDE
测试代码
package com.riverrat.recipebook;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(OrderAnnotation.class)
public class IngredientListTest {
    //do stuff
    
    @Test
    @Order(1)
    public void testIsEmpty(){//do stuff}
    
    @Test
    @Order(2)
    public void testAddBacon(){//do stuff}
    
    @Test
    @Order(3)
    public void testDeleteBaconByObject(){//do stuff}
    
    @Test
    @Order(4)
    public void testDeleteBaconByInt(){//do stuff}
    
    @Test
    @Order(5)
    public void testLoadAllIngredients(){//do stuff}
}
当前POM配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.riverrat</groupId>
    <artifactId>RecipeBook</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
            
    <packaging>jar</packaging>
  
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>
解决方案
  • 首先补全Maven Surefire插件配置,你当前POM没有显式指定该插件版本,低于3.0.0-M5版本的Surefire插件不支持JUnit 5的方法排序注解,在POM中添加如下配置即可:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
        </plugin>
    </plugins>
</build>
  • 排查Netbeans IDE的测试运行配置,部分旧版本Netbeans内置的JUnit 5运行器存在注解识别异常,可尝试升级Netbeans到最新稳定版,或者在测试运行配置中指定使用Maven测试命令执行,而非IDE内置运行器。
  • 也可以将JUnit 5版本升级到5.8.2以上,该版本修复了多个和Java 16兼容的注解解析问题,和现有代码完全兼容,只需要修改POM中junit-jupiter相关依赖的版本号即可。

内容的提问来源于stack exchange,提问作者RiverRat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 14:57:05