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

帅比萌擦擦* 提交于 2019-12-08 13:00:35

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/

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