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

旧时模样 提交于 2019-12-23 04:08:58

问题


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


回答1:


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/



来源:https://stackoverflow.com/questions/24168293/how-can-i-setup-my-environment-to-use-h2-for-tests-and-mysql-for-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!