【问题标题】:Init database before tests JUnit5 and Spring在测试 JUnit5 和 Spring 之前初始化数据库
【发布时间】:2019-06-28 16:40:08
【问题描述】:

我的项目使用 Spring、Hibernate ant JUnit 5。在所有测试之前初始化 DB 的最佳方法是什么?

这就是我厌倦的方式:

class DbCreatorService {
   @Autowired
   private Service1;
   @Autowired
   private Service2;
....
}

@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
@Transactional
class MyTest {

    @BeforeAll
    static void initDatabase(@Autowired DbCreatorService dbCreatorService ) {
        dbCreatorService.initDB()
    }
}

当我在initDB() 的某个地方调用sessionFactory.getCurrentSession() 时,我得到:org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread。 sessionFactory 被@Autowired 注入。

【问题讨论】:

  • initDB() 方法有什么作用?它是初始化与数据库的连接还是在测试之前准备好带有数据的表?
  • 它创建数据库并在表中插入数据
  • 我用更多细节更新了我的问题
  • 我一直在测试范围内使用嵌入式 H2 进行此操作。我有一些工作代码给你看。明天我会试着写一个答案。

标签: spring hibernate junit


【解决方案1】:

我的测试刚刚从数据库中读取,我只想在所有测试之前初始化一次数据库。我的最终解决方案:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
@Transactional
class MyTest {
    @Autowired 
    private DbCreatorService dbCreatorService;

    @BeforeAll
    void initDatabase() {
        dbCreatorService.initDB()
    }
}

【讨论】:

【解决方案2】:

我们正在努力实现什么?

一个测试友好的数据库场景,可以在每个测试用例之前和之后轻松设置。

我是如何做到的

我一直在测试范围内使用嵌入式 H2 数据库,在每个测试用例之前通过 SQL 脚本设置数据库。

  1. 首先,在 pom.xml 上添加 H2 on test scope 依赖项(您使用的是 Maven 对吗?)。

    <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency>

  2. 然后,设置您的 JUnit 测试类。

SampleTestClass.java

@SpringBootTest // Allows you to autowire beans on your test class

@AutoConfigureTestDatabase // Configures a database on test scope instead of your regular database config.

@RunWith(SpringRunner.class) // Runs test context with SpringRunner.class

@SqlGroup({ // Setups .sql files to be run on specific triggers. In this case, before each test method the script will be ran.
        @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:beforeTest.sql"),
})

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) // The spring application context will be considered "dirty" before each test method, and will be rebuilt. It means that
// your autowired beans will not carry any data between test cases.

public class SampleTestClass {

// write your test cases here...
}
  1. 在您的测试类路径中,添加一个beforeTest.sql 文件,该文件将在每个测试方法之前运行。该文件可能包括表创建和一些数据插入供您使用。如果你需要一些额外的数据库交互,你总是可以在你的测试类上使用 @Before 注释的方法。该方法将在每个测试用例之前运行。

【讨论】:

  • 感谢您的详细解释,但我需要一种在所有测试方法之前初始化 DB 的方法,而不是针对每种测试方法。另外我正在使用 JUnit5 而只是 Spring 而不是 SpringBoot
  • 很抱歉误解了您的要求。尽管如此,我相信您仍然可以使用其中一些技术来设置您的数据库进行测试。如果您需要在所有测试之前初始化数据库,就像您说的那样,JUnit @Before 注释是您最好的选择。 Spring 和 Spring boot 之间的唯一区别是上下文配置注释,我相信您已经在代码中整理了这些注释。
  • 我将@Before 与一个标志一起使用,所以我只初始化数据库一次,但这看起来像一个黑客。
  • 在测试套件上使用 before 进行常规设置是最优雅的解决方案。
  • 这个设置有用吗?你找到更优雅的解决方案了吗?
猜你喜欢
  • 2016-11-10
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2020-08-30
相关资源
最近更新 更多