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

Jenkins中Maven项目JUnit测试类未执行问题求助

Troubleshooting "No tests to run" in Maven & Jenkins for your Java Web Project

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 PreProcessorTest class marked as public?
  • 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.

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.java file?
  • 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.xml lives)?

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 just mvn clean, and no -DskipTests/-Dmaven.test.skip=true flags).
  • 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 test output (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 your pom.xml

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:37:29