嵌入式PostgreSQL for Java JUnit测试 [英] Embedded PostgreSQL for Java JUnit tests

查看:90
本文介绍了嵌入式PostgreSQL for Java JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在嵌入式PostgreSql,以便我们可以对PostgreSql驱动的应用程序进行单元测试?

Is there an embedded PostgreSql so that we could unit test our PostgreSql driven application?

由于PostgreSql有一些方言,所以最好使用嵌入式PostgreSql本身,而不是其他嵌入式数据库.

Since PostgreSql has some dialects, it's better to use embedded PostgreSql itself than other embedded databases.

嵌入式并不一定意味着它必须嵌入JVM进程中.它还不一定需要使用内存中的持久性.应该由依赖项管理(Maven,Gradle)自动加载它,以便单元测试可以在每台机器上运行,而不必安装和配置本地PostgreSQL服务器.

Embedded does not necessarily mean it must be embedded in the JVM process. It also does not necessarily need to use in-memory persistence. It should be loaded automatically by the dependency management (Maven, Gradle), so that Unit tests can run on every machine without having to install and configure a local PostgreSQL server.

推荐答案

不,就进程内可加载的数据库库而言,没有嵌入式PostgreSQL. PostgreSQL是面向过程的.每个后端都有一个线程,并产生多个进程来工作.作为图书馆没有意义.

No, there is no embedded PostgreSQL, in the sense of an in-process-loadable database-as-a-library. PostgreSQL is process oriented; each backend has one thread, and it spawns multiple processes to do work. It doesn' make sense as a library.

H2数据库支持

The H2 database supports a limited subset of the PostgreSQL SQL dialect and the use of the PgJDBC driver.

可以做的是 一个新的临时数据库,以 ,以使其与其他实例不冲突,运行测试,然后使用pg_ctl停止它,最后删除临时数据库.

What you can do is initdb a new temporary database, start it with pg_ctl on a randomized port so it doesn't conflict with other instances, run your tests, then use pg_ctl to stop it and finally delete the temporary database.

强烈建议,您在非默认端口上运行临时postgres ,因此您不必冒与运行测试的计算机上本地安装的任何PostgreSQL冲突的风险.

I strongly recommend that you run the temporary postgres on a non-default port so you don't risk colliding with any locally installed PostgreSQL on the machine running the tests.

(有 ecpg ,本质上是作为基于C语言扩展的预处理器嵌入在C 源代码中的PostgreSQL client ,它仍然需要运行中的服务器,使用起来有点讨厌,不推荐使用.它的存在主要是为了简化从其他各种数据库的移植.)

(There is "embedded PostgreSQL in the sense of ecpg, essentially a PostgreSQL client embedded in C source code as preprocessor based C language extensions. It still requires a running server and it's a bit nasty to use, not really recommended. It mostly exists to make porting from various other databases easier.)

这篇关于嵌入式PostgreSQL for Java JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆