【问题标题】:How to mock DB view with Spring dbunit?如何使用 Spring dbunit 模拟数据库视图?
【发布时间】:2019-01-17 12:48:08
【问题描述】:
我正在使用 Spring dbunit 编写集成测试。基本上,这很好,但我需要测试一个查询,但它使用我的数据库中的一个视图。我使用@DatabaseSetup 注释为xml 提供测试数据集。但是在这个 xml 中,我只能为 db 表配置行,当然,对于视图,这不起作用。有人知道如何在 db 视图中创建测试行并在测试中使用吗?
非常感谢。
【问题讨论】:
标签:
java
spring
testing
integration-testing
dbunit
【解决方案1】:
你有几个选择:
1) 您可以创建一个 JPA 实体并使用视图名称使用 @Table 注释。当然,视图是只读的,因此您将无法持久化实体。
2) 然后用 dbunit 将数据存入数据库:
i) Use an H2 in memory database and let hibernate create the database, the view will be created as a table, not a view.
ii) Use a real database and insert the data into the real tables behind the database.
看看这个answer