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

Spring Boot项目使用JUnit Categories无测试执行的问题求助

Troubleshooting JUnit Categories Not Running in Spring Boot

Let’s work through why your categorized tests aren’t executing as expected—this is a common issue with a few key gotchas to check off one by one.

1. Confirm JUnit Version & Annotation Match

First, make sure you’re using the right annotation for your JUnit version:

  • JUnit 4: Use @org.junit.experimental.categories.Category with marker interfaces (what you mentioned as "Categories").
  • JUnit 5: JUnit 4’s Category was replaced with @org.junit.jupiter.api.Tag—if you’re using Spring Boot’s default spring-boot-starter-test, it pulls in JUnit 5 by default, so Category won’t work here.

If you intend to use JUnit 4, you’ll need to exclude JUnit 5 from the starter dependency to avoid conflicts:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Add explicit JUnit 4 dependency -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

2. Verify Test Class & Method Marking

Double-check that your TestClassB is properly tagged with your Category interface:

// Your marker interface (e.g., in com.yourpackage.categories)
public interface IntegrationTests {}

// TestClassB
import org.junit.Test;
import org.junit.experimental.categories.Category;
import com.yourpackage.categories.IntegrationTests;

@Category(IntegrationTests.class) // Apply at class level to run all tests in the class
public class TestClassB {
    @Test
    public void testMethodInB() {
        // Your test logic
    }
}

You can also apply @Category to individual test methods if you only want specific ones to run.

3. Fix Maven Surefire Plugin Configuration

This is the most frequent culprit. Your pom.xml must explicitly tell the Surefire plugin which categories to include.

For JUnit 4:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version> <!-- Use a compatible version for JUnit 4 -->
            <configuration>
                <!-- Use the FULL qualified name of your Category interface -->
                <groups>com.yourpackage.categories.IntegrationTests</groups>
                <!-- Optional: Exclude categories if needed -->
                <!-- <excludedGroups>com.yourpackage.categories.UnitTests</excludedGroups> -->
            </configuration>
        </plugin>
    </plugins>
</build>

For JUnit 5 (using Tags instead of Categories):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version> <!-- Compatible with JUnit 5 -->
            <configuration>
                <properties>
                    <includeTags>IntegrationTests</includeTags> <!-- Match your @Tag value -->
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

4. Ensure Test Class Naming Follows Surefire Rules

By default, Surefire only runs classes named:

  • *Test.java
  • Test*.java
  • *TestCase.java

If your TestClassB doesn’t fit this pattern, add an explicit include rule to the Surefire configuration:

<configuration>
    <!-- ... existing category/tag config ... -->
    <includes>
        <include>**/TestClassB.java</include>
    </includes>
</configuration>

5. Validate Your Maven Command

Run the correct command based on your test type:

  • Unit tests (Surefire): mvn test
  • Integration tests (Failsafe, if you’re using it): mvn verify

6. Rule Out Dependency Conflicts

Run mvn dependency:tree to check for conflicting JUnit versions. If both JUnit 4 and 5 are present, it can cause annotation processing to fail silently.

Quick Sanity Check

Try running the categorized tests directly in your IDE (e.g., IntelliJ/Eclipse). If they run there but not via Maven, the problem is definitely in your Surefire plugin configuration.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:50:52