如何使用Spring dbunit模拟数据库视图?

时间:2019-01-17 12:48:08

标签: java spring testing integration-testing dbunit

我正在使用Spring dbunit编写集成测试。基本上,这很好,但是我需要测试一个查询,但是它在我的数据库中使用了一个视图。我使用@DatabaseSetup注释为xml提供测试数据集。但是,在此xml中,我只能为db表配置行,因为视图当然不起作用。有人知道如何在db视图中创建测试行并在测试中使用吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您有两种选择:

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