0

I'm new to databases, but I'm going to use PostgreSQL in my application with Hibernate. It's rather clear how to install Postgres, bind with hibernate and use it. But I can't figure out how to run in-memory Postgres database with maven for testing. Is it possible? If no - any advises how to test my application in this case?

p.s. links to the corresponding tutorials are the most desirable and I'll highly appreciate them. Thanks!

Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
StackNRG
  • 79
  • 1
  • 1
  • 6
  • 2
    Short answer: you can't. Postgres is not designed for an in-process deployment model. Just have a default server around with a default user that the unit tests can use. –  Jan 22 '14 at 17:50

1 Answers1

3

(I've revised and expanded this answer on the post this one is a dup of; I'm retaining this here as it's a more direct answer to this question).

But I can't figure out how to run in-memory Postgres database with maven for testing. Is it possible?

No, it is not possible. PostgreSQL is implemented in C and compiled to platform code. You can't shove it in a jar and fire it up as a throwaway in-memory DB.

Instead, have a standard test config and document it. Think:

To run these tests you'll need PostgreSQL 9.2 or newer installed and running on localhost port 5432, with a user named 'myapptest' who owns a database named 'myapptest'. 'md5' authentication must be permitted with password 'abcdefg', or you can use 'trust' mode.

To set this up you might use:

  CREATE USER myapptest PASSWORD 'abcdefg';
  CREATE DATABASE myapptest OWNER myapptest TEMPLATE template0;

and add an entry in pg_hba.conf like:

  host    myapptest    myapptest    127.0.0.1/32    md5
  host    myapptest    myapptest    ::1/128         md5

Use a Maven parameter to control whether tests that hit the database get run, so that the rest of the tests can still run.

You can probably use Maven params to specify a DB hostname/username/dbname/password for testing too, if you're keen. I wouldn't bother personally.

Alternately, if you're really keen you could have your test harness locate the initdb and postgres binaries, run initdb to create a database, modify pg_hba.conf to trust, run postgres to start it on a random port, create a user, create a DB, and run the tests. Personally I think that's a major pain that should be avoided; it's way easier to just have a test DB configured.

Some people instead use the H2 database in PostgreSQL dialect mode to run tests. I think that's almost as bad as the Rails people using SQLite for testing and PostgreSQL for development.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • 1
    "I think that's almost as bad as the Rails people using SQLite for testing and PostgreSQL for development." -- I agree, up to a point. I think that if your testing suite accepts a build intended to run on PostgreSQL without testing it on PostgreSQL, then you've done something wrong. However, I also think that if you wanted to use H2 in some test setups as part of your testing suite, that's less crazy. For example, integration tests using H2 versus acceptance tests using PostgreSQL could be a good setup for some projects. – sigpwned Apr 20 '18 at 15:59