Janning Vygen
Janning Vygen

Reputation: 9222

How to start a postgresql instance for integration testing

Usually I use hsqldb for some integration tests and it works fine. But some test need to have a postgresql instance. As our production server run postgresql it is a good idea anyway to run the test against a production database.

Is there a maven plugin or something similar which can easily install and start a postgresql database on a given port and shut it down after all test are run?

Something like mysql-je for mysql?

Upvotes: 7

Views: 2772

Answers (2)

Janning Vygen
Janning Vygen

Reputation: 9222

Testcontainers is the answer. Easy to configure and just does its job. For Postgresql they have a module:

https://www.testcontainers.org/modules/databases/postgres/

And this way you have your test system as close as possible to the production system, which I think is a pretty good idea.

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 47243

I don't know of anything. But you probably don't want to start and stop the actual PostgreSQL server; you want to have the server running all the time, and create and destroy databases as needed.

You can create a database in SQL, and also destroy it. If you set up an initial database that is empty, and not used for storing any data, you can have a workflow like:

  1. Connect to the empty database
  2. Issue the command to create a new database
  3. Run tests against the new database
  4. Issue the command to drop the new database

Upvotes: 4

Related Questions