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

Spring Boot功能测试中使用TestEntityManager报错求助

问题分析与解决方案

问题根源

1. 测试注解冲突

@SpringBootTest 和 @DataJpaTest 不能同时使用:

  • @DataJpaTest 是JPA切片测试注解,已经内置了Spring Boot测试上下文配置,会自动初始化TestEntityManager、嵌入式数据库等JPA相关组件,无需额外启动完整Web环境。
  • @SpringBootTest 是全栈功能测试注解,用于启动完整的Spring应用上下文(包括Web环境)。同时使用两个注解会导致上下文加载逻辑冲突,触发依赖版本不匹配问题。

2. 依赖版本不兼容

你手动指定了spring-webflux、reactor-netty-http等依赖的版本,而Spring Boot 2.7.0的dependencyManagement已经统一管理了这些组件的兼容版本,手动指定版本会破坏依赖一致性,导致BootstrapMode(Spring Data核心类)无法找到。

具体修复步骤

步骤1:修正测试类注解

根据你的测试需求二选一:

场景A:功能测试(需要启动Web环境)

移除@DataJpaTest,仅保留@SpringBootTest,同时确保依赖中包含spring-boot-starter-data-jpa:

@RunWith(SpringRunner.class)
@SpringBootTest(
        classes = ApplicationFunctionalTests.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class BaseFunctionalTest {

    @Autowired
    protected TestEntityManager testEntityManager;

    // ... 其他测试逻辑
}

场景B:仅测试JPA层(无需Web环境)

移除@SpringBootTest,仅保留@DataJpaTest:

@RunWith(SpringRunner.class)
@DataJpaTest
public abstract class BaseFunctionalTest {

    @Autowired
    protected TestEntityManager testEntityManager;

    // ... 其他测试逻辑
}

步骤2:统一依赖版本管理

修改pom.xml,移除手动指定的组件版本,让Spring Boot自动管理兼容版本:

<dependencies>
    <!-- test dependencies -->
    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock-jre8</artifactId>
        <version>2.27.2</version>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.5.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.19.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.17.3</version>
        <scope>test</scope>
    </dependency>
    <!-- 移除手动指定的version,由Spring Boot管理 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.netty</groupId>
        <artifactId>reactor-netty-http</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 移除重复的reactor-netty-http依赖 -->
    <!-- 移除手动指定的jakarta.validation-api,spring-boot-starter-test已包含兼容版本 -->
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

步骤3:确保TestEntityManager依赖存在

如果你的主项目未引入spring-boot-starter-data-jpa,需要在测试依赖中添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <scope>test</scope>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:50:27