[英]Is there a way to reload application.yml before test class?
I'm trying to test a method with @SpringBootTest and @ActiveProfile("test") in my class.我正在尝试在我的 class 中使用 @SpringBootTest 和 @ActiveProfile("test") 测试方法。 When I run the new tests, everything works fine and the properties are loaded from the test YML file correctly.
当我运行新测试时,一切正常,并且从测试 YML 文件中正确加载了属性。 But when I run all tests, those same tests load the properties from the production YML file.
但是当我运行所有测试时,这些相同的测试会从生产 YML 文件中加载属性。
I've tried using @DirtiesContext, with BEFORE_CLASS and BEFORE_EACH_METHOD flags, on the new test class but it doesn't seem to help.我已经尝试在新的测试 class 上使用带有 BEFORE_CLASS 和 BEFORE_EACH_METHOD 标志的@DirtiesContext,但它似乎没有帮助。
I have something similar to the following:我有类似以下内容:
@ActiveProfiles("test")
@SpringBootTest
public class NewTestClass{
...
@Test
public void testThatLoadsPropertiesAndShouldLoadApplicationTestYmlFile() {
...
}
}
What am I missing here?我在这里想念什么? is there a simple way of solving this problem?
有没有解决这个问题的简单方法?
UPDATE更新
I disabled the other Integration Tests on the project using @Disable from Junit 5 and checked if the problem persisted.我使用 Junit 5 中的 @Disable 禁用了项目中的其他集成测试,并检查问题是否仍然存在。 surprisingly, it did.
令人惊讶的是,确实如此。 So I'm suspecting it is not an
ApplicationContext
caching problem.所以我怀疑这不是
ApplicationContext
缓存问题。
Having the problem description provided I can only suggest the that there might be another test suite configured so that the context is setup using the production config (yml).提供了问题描述后,我只能建议可能配置了另一个测试套件,以便使用生产配置(yml)设置上下文。 Please consult this answer (the question raised is similar in a way...): https://stackoverflow.com/a/43332643/8085853
请参考这个答案(提出的问题在某种程度上是相似的......): https://stackoverflow.com/a/43332643/8085853
Ok, I believe I found the cause of the problem.好的,我相信我找到了问题的原因。 I'm running all tests using IntelliJ, but "all tests" is being managed by the JUnit plugin and the single tests are being managed by the Gradle plugin.
我正在使用 IntelliJ 运行所有测试,但“所有测试”由 JUnit 插件管理,单个测试由 Gradle 插件管理。 Once I used gradle to run "all tests", everything worked.
一旦我使用 gradle 运行“所有测试”,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.