我如何设置我的环境使用h2测试和mysql进行开发? [英] How can I setup my environment to use h2 for tests and mysql for development?
问题描述
我使用 yo jhipster
生成器的默认配置,除了我使用mysql作为我的dev数据库。当我运行 mvn test
时,我的测试成功,没有失败。
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.
但是,我发现如果我运行测试第二次,套件将失败,因为它将针对dev数据库运行...在上一次测试运行后没有回滚或重置。我宁愿期望它运行在内存h2数据库中配置在 src / test / resources / config / application.yml
,将在每次运行后重置。
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.
如何使用h2设置我的环境用于测试和mysql的开发?
How can I setup my environment with h2 for tests and mysql for development?
推荐答案
我不知道这是否是正确的方式来解决这个问题,但我能够让我的测试通过重复运行通过创建一个新的配置文件测试。
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".
然后我必须做两件事:
1)更改 spring.profile
在src / test / resources / config / application.yml中test而不是dev(使测试application.yml与dev不同)
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)使用 @ActiveProfiles(test)
而不是 @ActiveProfiles(dev)
在我的测试中
2) use @ActiveProfiles("test")
instead of @ActiveProfiles("dev")
in my tests
测试application.yml使用h2数据库,并根据需要在运行之间重置。
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.
请参阅 https:// spring.io/guides/tutorials/data/3/
这篇关于我如何设置我的环境使用h2测试和mysql进行开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!