SpringBoot事务@Test如何获取当前session?

问题描述 投票:0回答:2

Spring 文档Transactional 测试中警告

False Positives
并提出以下建议:

// ...

@Autowired
SessionFactory sessionFactory;

@Transactional
@Test // no expected exception!
public void falsePositive() {
    updateEntityInHibernateSession();
    // False positive: an exception will be thrown once the Hibernate
    // Session is finally flushed (i.e., in production code)
}

@Transactional
@Test(expected = ...)
public void updateWithSessionFlush() {
    updateEntityInHibernateSession();
    // Manual flush is required to avoid false positive in test
    sessionFactory.getCurrentSession().flush();
}

// ...

我有以下基类:

@SpringBootTest
@Transactional
@AutoConfigureMockMvc
public abstract class BaseSpringBootTest {

和一个扩展它的类,我想应用这种注入

sessionFactory
的做法:

public class EmployeeRepositoryTest extends BaseSpringBootTest {

  @Autowired
  SessionFactory sessionFactory

但我得到:

NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available

我也试过注射

@Autowired
EntityManagerFactory entityManagerFactory;

然后打电话:

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
sessionFactory.getCurrentSession();

但这会引发以下异常:

org.hibernate.HibernateException: No CurrentSessionContext configured!

我如何在测试中获得对

currentSession
的引用,以便我最终可以调用:

sessionFactory.getCurrentSession().flush();

如 Spring Boot 文档中所述?

spring spring-boot hibernate junit spring-test
2个回答
0
投票

你只需要继续阅读/小卷轴:

下面的例子展示了JPA的匹配方法:

@PersistenceContext EntityManager entityManager;

@Transactional
@Test // no expected exception!
public void falsePositive() {
    updateEntityInJpaPersistenceContext();
    // False positive: an exception will be thrown once the JPA
    // EntityManager is finally flushed (i.e., in production code)
}

@Transactional
@Test(expected = ...)
public void updateWithEntityManagerFlush() {
    updateEntityInJpaPersistenceContext();
    // Manual flush is required to avoid false positive in test
    entityManager.flush();
}

这应该解决(所有)你的:

org.hibernate.HibernateException: No CurrentSessionContext configured!

..问题。


0
投票

看看这个技术,要访问

SessionFactory
并获取对测试中当前会话的引用,您需要配置必要的 beans 并提供所需的依赖项。这是您可以遵循的方法:

  1. 确保您的项目中具有必要的依赖项。在您的

    spring-boot-starter-data-jpa
    pom.xml
    文件中包含
    build.gradle
    依赖项。

  2. 在测试配置中配置

    SessionFactory
    bean。您可以创建一个单独的配置类用于测试目的。例如:

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;

@Configuration
public class TestConfiguration {
    
    @Autowired
    private EntityManagerFactory entityManagerFactory;
    
    @Bean
    public SessionFactory sessionFactory() {
        return entityManagerFactory.unwrap(SessionFactory.class);
    }
    
    @Bean
    public HibernateJpaSessionFactoryBean sessionFactoryBean() {
        return new HibernateJpaSessionFactoryBean();
    }
}
  1. 在你的测试类
    EmployeeRepositoryTest
    中,用
    @ContextConfiguration
    注释并指定测试配置类:
@ContextConfiguration(classes = TestConfiguration.class)
public class EmployeeRepositoryTest extends BaseSpringBootTest {
    
    @Autowired
    private SessionFactory sessionFactory;
    
    // Your tests and assertions...
}

按照这些步骤,您应该能够访问

SessionFactory
和测试课程中的当前会话。请记住包含必要的依赖项并确保正确设置测试配置以提供
SessionFactory
bean。

© www.soinside.com 2019 - 2024. All rights reserved.