gpt4 book ai didi

java - 使用 hsql hibernate 数据源隔离 Junit 测试

转载 作者:行者123 更新时间:2023-11-30 06:16:19 26 4
gpt4 key购买 nike

我正在为我的 Spring 应用程序编写 Junit 测试。当我单独运行每个测试时,它可以工作,但是当我将它们作为一个整体运行时,每个测试的数据都会保留。测试使用 hibernate 实体生成的 hsql 表。有没有办法隔离测试,以便每个测试都处理空表?

谢谢!

最佳答案

您可以将测试方法或测试类标记为@Transactional:

Annotating a test method with @Transactional causes the test to be run within a transaction that will, by default, be automatically rolled back after completion of the test. If a test class is annotated with @Transactional, each test method within that class hierarchy will be run within a transaction.

更多信息:1 , 2

关于java - 使用 hsql hibernate 数据源隔离 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49139724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com