求助:mvn test已编译全部5个测试类但仅执行其中1个的原因排查
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, returnvoid, and carry the@org.junit.Testannotation. 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.Testannotation. Also, make sure you’re not mixing JUnit 4 and 5 annotations accidentally.
- For JUnit 4: Test methods must be
Unexpected Surefire plugin configuration
Check yourpom.xmlfor explicit<includes>/<excludes>rules in themaven-surefire-pluginsetup. 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 runningmvn test -Xfor verbose debug output, or check thetarget/surefire-reportsdirectory. 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:
- Run
mvn test -Xto look for lines mentioning "Skipping test class" or explanations for excluded classes - Inspect the
target/surefire-reportsfolder for per-class execution details - Verify your
maven-surefire-pluginversion and configuration inpom.xml
内容的提问来源于stack exchange,提问作者Lavair

