postgresql - 仅将 H2 数据库用于单元测试

标签 postgresql unit-testing spring-boot h2

我有一个基于 Postgres 数据库的 spring boot 应用程序。

现在我想单独使用h2数据库进行单元测试。

这样做对吗?或者有什么建议

最佳答案

是的,您还应该使用 H2 作为内存数据库,因为它可以足够快地创建一个干净的数据库,针对 执行单元测试>在执行测试周期阶段时,足够快地删除数据库
在每次构建时创建和删除物理数据库会消耗大量时间,并且会使您的本地构建变慢

现在,自动化测试不应该只依赖H2
这有一些限制,与您的目标 DBMS 相比,这些限制可能会产生轻微不同的行为 (PostgreSQL)。
您还应该创建使用目标 DBMS 的集成测试。
通常,这些集成测试不应在开发人员构建时自动执行,而应在持续集成环境中执行。

H2 兼容性和限制:

H2 提供了一些特定的数据库兼容性模式(适用于 PostgreSQL 和许多其他数据库),但这些模式有多个极端情况。

它不完全支持 ANSI SQL 和特定的数据库特性:

Compatibility

All database engines behave a little bit different. Where possible, H2 supports the ANSI SQL standard, and tries to be compatible to other databases. There are still a few differences however:

In MySQL text columns are case insensitive by default, while in H2 they are case sensitive. However H2 supports case insensitive columns as well. To create the tables with case insensitive texts, append IGNORECASE=TRUE to the database URL (example: jdbc:h2:~/test;IGNORECASE=TRUE).

你还可以得到一些不详的信息on this page ,关于具体的具体数据库模式:

Compatibility Modes

For certain features, this database can emulate the behavior of specific databases. However, only a small subset of the differences between databases are implemented in this way. Here is the list of currently supported modes and the differences to the regular mode:

DB2 Compatibility Mode

...

MySQL Compatibility Mode

...

Oracle Compatibility Mode

...

PostgreSQL Compatibility Mode

To use the PostgreSQL mode, use the database URL jdbc:h2:~/test;MODE=PostgreSQL or the SQL statement SET MODE PostgreSQL.

For aliased columns, ResultSetMetaData.getColumnName() returns the alias name and getTableName() returns null. When converting a floating point number to an integer, the fractional digits are not be truncated, but the value is rounded. The system columns CTID and OID are supported. LOG(x) is base 10 in this mode.

关于postgresql - 仅将 H2 数据库用于单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294478/

相关文章:

c# - 以表格格式返回多个 PostgreSQL 值

python - SQLAlchemy 错误 : "argument formats can' t be mixed"when inputting variables

c# - 用于单元测试的最小起订量服务定位器

ruby-on-rails - 如何从子模型中总结父模型记录

postgresql - Postgres 9.3 的 Docker 入口点

asp.net-mvc - 使用存储库模式的 ASP.NET MVC 单元测试

unit-testing - react-navigation-hooks : How to test useFocusEffect

java - 如何使用 springboot findAll() 方法编写 not in 子句

java - 即使在 Spring boot 中传递了 args 之后,也使用 applicationContext 调用 noArgs 构造函数创建 bean

spring-boot - Jaeger UI 中未找到服务依赖项