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

Spring Boot 3.0.0中使用@DataJpaTest导致测试发现错误

Spring Boot 3.0.0下@DataJpaTest测试发现失败的解决方案

问题描述

在Spring Boot 3.0.0环境中,给测试类添加@DataJpaTest注解后,测试执行失败,出现JUnit测试发现错误。未添加该注解及@ExtendWith(SpringExtension.class)时测试可正常执行,Spring Boot 2.7.x版本无此问题。执行mvn test命令时Maven会跳过该测试,在IntelliJ 2022.3中运行则触发错误。

测试代码

import com.owino.paginateddata.entities.Tweet;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@DataJpaTest
@ExtendWith(SpringExtension.class)
public class PersistTweetTest {

    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void shouldPersistTweetTest(){
        var entity = entityManager.persistAndFlush(new Tweet(1L,"#goodvibes #sweetypie", "2022-12-11","11:00PM"));

        Assertions.assertThat(entity).isNotNull();
        Assertions.assertThat(entity.getId()).isEqualTo(1L);
        Assertions.assertThat(entity.getContents()).isEqualTo("#goodvibes #sweetypie");
        Assertions.assertThat(entity.getDateString()).isEqualTo("2022-12-11");
        Assertions.assertThat(entity.getTimeString()).isEqualTo("11:00PM");
    }
}

报错信息

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:132)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:78)
    at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.junit.platform.commons.JUnitException: ClassSelector [className = 'com.owino.paginateddata.persistence_test.PersistTweetTest'] resolution failed
    at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:103)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:83)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:46)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
    ... 13 more

解决方案

1. 移除冗余注解

Spring Boot 3.x版本的@DataJpaTest已经默认集成了SpringExtension,无需手动添加@ExtendWith(SpringExtension.class),重复添加会引发兼容性冲突。修改后的测试类只需保留@DataJpaTest:

import com.owino.paginateddata.entities.Tweet;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;

@DataJpaTest
public class PersistTweetTest {

    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void shouldPersistTweetTest(){
        var entity = entityManager.persistAndFlush(new Tweet(1L,"#goodvibes #sweetypie", "2022-12-11","11:00PM"));

        Assertions.assertThat(entity).isNotNull();
        Assertions.assertThat(entity.getId()).isEqualTo(1L);
        Assertions.assertThat(entity.getContents()).isEqualTo("#goodvibes #sweetypie");
        Assertions.assertThat(entity.getDateString()).isEqualTo("2022-12-11");
        Assertions.assertThat(entity.getTimeString()).isEqualTo("11:00PM");
    }
}

2. 对齐JUnit Jupiter版本

Spring Boot 3.0.0要求JUnit Jupiter版本至少为5.9.0,确保依赖管理中JUnit版本与Spring Boot的默认配置一致。Maven项目中无需手动指定JUnit版本,通过Spring Boot parent依赖自动管理即可:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version>
    <relativePath/>
</parent>

3. 验证测试类路径

确保测试类位于src/test/java目录下,且包结构与主代码匹配,保证Spring Boot测试自动配置能正确扫描到测试类。

4. 清理缓存并重新构建

  • 执行Maven命令清理缓存并重新测试:mvn clean test
  • IntelliJ中清理IDE缓存:点击File -> Invalidate Caches...,选择"Invalidate and Restart"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 13:00:46