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

Spring Boot+Hibernate运行JUnit测试时createEntityManagerFactory报错

Spring Boot 结合 Hibernate 测试阶段 ApplicationContext 加载失败问题

生产环境运行正常的Spring Boot+Hibernate API,编写JUnit测试时出现如下错误:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
具体报错:java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@6c1e40d9 testClass = com.witcher.witcher_api.CharacterRepoTest

相关代码与配置

Repository 实现类

@Component
@Repository
public class CharacterRepositoryJdbcImpl implements CharacterRepository{

   private EntityManagerFactory emf;
   private EntityManager entityManager;

    @Autowired
    public CharacterRepositoryJdbcImpl() throws ClassNotFoundException {
        System.out.println(getClass().getResource("META-INF/persistence.xml"));
           // 错误发生在下方这行
        this.emf = Persistence.createEntityManagerFactory("wticher-pg");
        this.entityManager = emf.createEntityManager();
    }
}

测试类代码

@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureTestDatabase
public class CharacterRepoTest {


    @Autowired
    private CharacterRepository characterRepositoryJdbcImpl;

    @Test
    @Transactional
    public void testGetCharacter(){
        Character testCharacter = characterRepositoryJdbcImpl.findCharacterById(testCharacterId01);
        assertEquals( "Get character by id","character_01", "testname");
    }
}

生产环境 persistence.xml 配置

<persistence version="2.2"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
     http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

    <persistence-unit
            name="wticher-pg">
        <class>com.witcher.witcher_api.model.pojo.User</class>
        <class>com.witcher.witcher_api.model.pojo.Character</class>
        <class>com.witcher.witcher_api.model.pojo.Weapon</class>
        <class>com.witcher.witcher_api.model.pojo.Armor</class>
        <properties>
            <property
                    name="javax.persistence.jdbc.url"
                    value="jdbc:postgresql://localhost:5432/witcher_pg"
            />
            <property
                    name="javax.persistence.jdbc.user"
                    value="witcher_admin"
            />
            <property
                    name="javax.persistence.jdbc.password"
                    value="witcherpass"
            />
            <property
                    name="hibernate.show_sql"
                    value="true"
            />

        </properties>
    </persistence-unit>
</persistence>

已尝试操作

  • persistence.xml 位于 src/main/resources/META-INF/,生产环境运行正常
  • 已在 test/resources 下添加 application-test.properties 及对应环境的 persistence.xml
  • 问题仅出现在测试阶段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:15:07