【发布时间】:2023-01-12 19:38:14
【问题描述】:
我一直在抱怨我必须在没有自动化的情况下用眼睛检查 Hibernate 查询。这就是为什么我对 vladmihalcea 的名为 Hypersistence Utils 的库着迷。
但这就是问题所在。我无法使用它的方法。我使用 Kotlin Spring Boot。 下面是源代码:
实体
@Entity
class Book(
@Id
@GeneratedValue(strategy = IDENTITY)
val id: Long = 0L,
)
资料库
interface BookRepository : JpaRepository<Book, Long>
服务
@Service
class DemoService(
private val bookRepository: BookRepository,
) {
@Transactional
fun a() {
bookRepository.findById(0)
}
}
最后是测试代码
@SpringBootTest
@TestConstructor(autowireMode = ALL)
internal class DemoServiceTest(
private val demoService: DemoService,
) {
@Test
fun a() {
reset()
demoService.a()
assertSelectCount(1)
}
}
此测试给我一个失败的结果,并显示以下消息:
Expected 1 statements but recorded 0 instead!
com.vladmihalcea.sql.exception.SQLSelectCountMismatchException: Expected 1 statements but recorded 0 instead!
我不明白的是,正在测试的服务方法a() 是事务性方法,但assertSelectCount() 方法未检测到已执行的查询。
这是日志:
[ Test worker] org.hibernate.SQL : select book0_.id as id1_0_0_ from book book0_ where book0_.id=?
[ Test worker] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [0]
请你帮助我好吗? 提前致谢。
【问题讨论】:
标签: spring-boot hibernate kotlin jpa