问题描述
我的问题是,当我分别运行测试时,测试可以正常运行,但是当我运行所有测试时,多个测试开始失败.
my problem is that when I run tests individually the test work properly but when I run all the tests several tests start to fail.
似乎有一个applicationContext问题,我有3个applicationContext,但是applicationContext-test之前导入了2个.
It looks to be a problem of the applicationContext I have 3 applicationContext but the applicationContext-test import the 2 before.
有时候我会@applicationContext(applicatonContext1.xml)
Some times I do @applicationContext(applicatonContext1.xml)
和其他时间@applicationContext(applicationContext-test.xml)
and other times@applicationContext(applicationContext-test.xml)
对于不同的测试,我会得到不同的例外情况:
I get different exceptions for different tests:
- SQLGrammarException:无法执行查询.
- 独立实体异常
- ConstraintViolation异常.
有些测试工作正常,有些没有,但是我不知道为什么.谢谢
Some tests work properly and some no, but I don't know why. Thanks
推荐答案
在测试中放入DirtiesContext
,以允许每次运行测试时重新加载上下文.
PutDirtiesContext
on test to allow context to be reloaded each time you run test.
不同的@ApplicationContext
应该可以正常工作.我认为您没有在测试中清除数据库(因为没有@DirtiesContext
spring会为相同的@ApplicationContext
使用相同的上下文(和相同的DB)).
Different @ApplicationContext
should work fine. I think you not clean the database in your test (because without @DirtiesContext
spring will use same context (and same DB) for same @ApplicationContext
).
这篇关于jpa使用spring运行多个测试的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!