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

在Mockito+Spring MVC中Mock HibernateSession时无法找到该类的问题

解决HibernateSession类无法解析的问题

你代码里使用的HibernateSession并非Hibernate官方提供的类,Hibernate中负责数据库会话操作的核心类是org.hibernate.Session——你添加的hibernate-core和hibernate-entitymanager依赖里只有Session类,找不到HibernateSession是正常的。

具体解决步骤:

  • 替换类引用,把代码中的HibernateSession改为org.hibernate.Session:
    @Mock private org.hibernate.Session hibernateSession;
    
    或者提前导入包后简写:
    import org.hibernate.Session;
    
    // ...
    @Mock private Session hibernateSession;
    
  • 若需要Mock SessionFactory,同样使用Hibernate官方的org.hibernate.SessionFactory类:
    @Mock private SessionFactory sessionFactory;
    
  • 确认测试依赖中包含Mockito(因为用到了@Mock注解),未添加的话补充:
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 06:10:03