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

求助:mvn test已编译全部5个测试类但仅执行其中1个的原因排查

Possible Reasons for Only One Test Class Executed in mvn test

Since you’ve confirmed your test class filenames follow Maven Surefire’s naming conventions (like ending with Test/Tests or starting with Test) and the compiler is picking up all 5 source files, here are the most likely culprits to investigate:

  • Invalid test method setup
    Even if the class compiles, Surefire will only execute it if it contains valid test methods. Double-check the 4 skipped classes:

    • For JUnit 4: Test methods must be public, return void, and carry the @org.junit.Test annotation. A class with no methods meeting this criteria won’t run.
    • For JUnit 5: Methods don’t need to be public, but must have @org.junit.jupiter.api.Test annotation. Also, make sure you’re not mixing JUnit 4 and 5 annotations accidentally.
  • Unexpected Surefire plugin configuration
    Check your pom.xml for explicit <includes>/<excludes> rules in the maven-surefire-plugin setup. It’s easy to accidentally add a rule that only includes the one test class that’s running. For example:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M7</version>
      <configuration>
        <includes>
          <include>**/WorkingTest.java</include>
        </includes>
      </configuration>
    </plugin>
    

    Even a small typo here could exclude unintended classes.

  • Class-level skip annotations
    If the skipped test classes have a class-level @Ignore (JUnit 4) or @Disabled (JUnit 5) annotation, the entire class will be skipped silently. Double-check the class headers for these markers.

  • Test class initialization failures
    If a test class has a @BeforeClass (JUnit 4) or @BeforeAll (JUnit 5) method that throws an uncaught exception, Surefire will skip all methods in that class. This failure might not show up in the main console log—try running mvn test -X for verbose debug output, or check the target/surefire-reports directory. Each test class has a separate report file that will explain why it was skipped.

  • Nested/inner test class limitations
    If your skipped classes are inner/nested classes, older versions of Surefire might not recognize them. JUnit 5 supports nested tests, but you’ll need a Surefire plugin version of at least 3.0.0-M4 to scan for them automatically.

To narrow this down faster:

  1. Run mvn test -X to look for lines mentioning "Skipping test class" or explanations for excluded classes
  2. Inspect the target/surefire-reports folder for per-class execution details
  3. Verify your maven-surefire-plugin version and configuration in pom.xml

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 05:32:46