(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.