Jenkins中Maven项目JUnit测试类未执行问题求助
Hey there! Let’s figure out why your PreProcessorTest.java runs fine in IntelliJ but won’t execute with mvn test or Jenkins—super frustrating, I know. Let’s break down the most likely issues and fixes step by step:
1. Double-check your test class & method basics
Maven’s Surefire plugin is picky about test conventions, even if IntelliJ is lenient:
- Is your
PreProcessorTestclass marked aspublic? - Are your test methods
public void(no return type) and annotated with the correct@Test?- For JUnit 4, use
org.junit.Test - For JUnit 5, use
org.junit.jupiter.api.Test - Mixing these annotations with mismatched dependencies is a super common gotcha.
- For JUnit 4, use
2. Verify your Maven Surefire plugin & test dependencies
The Surefire plugin handles running tests in Maven—misconfigurations or missing dependencies will make it skip tests entirely.
For JUnit 5 projects
Make sure your pom.xml includes these critical bits:
<dependencies> <!-- Required engine to run JUnit 5 tests --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <!-- Use the latest stable version --> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- Surefire plugin compatible with JUnit 5 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <!-- Minimum 2.22.0 for JUnit 5 support --> </plugin> </plugins> </build>
For JUnit 4 projects
Use this setup instead:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> </plugin> </plugins> </build>
Check for accidental exclusions
Look for any includes/excludes in your Surefire config that might block your test. For example, this would skip all test classes entirely:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <excludes> <exclude>**/*Test.java</exclude> <!-- Oops! This excludes your test --> </excludes> </configuration> </plugin>
3. Debug the Maven test command locally
Run mvn test -X (the -X enables detailed debug logging) and look for these clues:
- Does the log show Surefire scanning your
src/test/java/PreProcessorTest.javafile? - Are there errors about missing annotations, classpath issues, or failed class loading?
- Are you running the command from your project’s root directory (where the
pom.xmllives)?
Also, check if you’re using a Maven profile that skips tests (e.g., mvn test -P production where the profile has skipTests=true).
4. Validate Jenkins configuration
- Ensure your Jenkins build step uses
mvn clean test(not justmvn clean, and no-DskipTests/-Dmaven.test.skip=trueflags). - Confirm Jenkins uses the same Maven version as your local machine (check Manage Jenkins > Global Tool Configuration). Mismatched versions can cause dependency or plugin conflicts.
- Look at the Jenkins console log’s Surefire section—does it mirror the local
mvn testoutput (e.g., "No tests to run" or specific errors)?
Next steps
To narrow this down further, share:
- The section of your Jenkins console log starting from
[INFO] --- maven-surefire-plugin:xxx:test (default-test) @ your-project --- - The
<dependencies>and<build>sections of yourpom.xml
内容的提问来源于stack exchange,提问作者Abhishek P

