sdoxsee
sdoxsee

Reputation: 4701

How can I setup my environment to use h2 for tests and mysql for development?

I'm using the default configuration for the yo jhipster generator except that I'm using mysql as my "dev" database. When I run mvn test my tests succeed with no failures.

However, I found that if I ran tests a second time, the suite would fail since it would run against the "dev" database...that wasn't 'rolled back' or 'reset' after the previous test run. I would rather have expected it to run against an in-memory h2 database as configured in src/test/resources/config/application.yml that would be reset after each run.

How can I setup my environment with h2 for tests and mysql for development?

Thanks

Upvotes: 1

Views: 2181

Answers (1)

sdoxsee
sdoxsee

Reputation: 4701

I'm not sure if this is the "right" way to solve this but I was able to get my tests to pass on repeated runs by creating a new profile "test".

I then had to do 2 things:

1) change spring.profile in src/test/resources/config/application.yml to "test" instead of "dev" (to make the test application.yml different from the dev one)

2) use @ActiveProfiles("test") instead of @ActiveProfiles("dev") in my tests

The test application.yml uses an h2 database and is reset between runs as desired.

Note: I also had some success with successive test runs without creating a new profile by annotating my test classes with:

@Transactional
@TransactionConfiguration(defaultRollback = true)

as...

At the end of the test the transaction will be rolled back and the data discarded leaving a fresh environment for the next test to execute.

see https://spring.io/guides/tutorials/data/3/

Upvotes: 1

Related Questions