简体   繁体   English

有没有办法在测试 class 之前重新加载 application.yml?

[英]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.

相关问题 SpringBootTest for an arbitrary ApplicationConfiguration class 测试application.yml文件的读取 - SpringBootTest for an arbitrary ApplicationConfiguration class to test the reading of the application.yml file Spring-无法在单元测试中的@Component序列化程序中加载application.yml(在测试类@Value中已正确加载) - Spring - cannot load application.yml in @Component serializer in unit test (In the test class @Value is loaded correctly) 在初始化SpringBootApplication之前寻找一种方法来解析springboot application.yml文件中的值 - Looking for a way to parse values in springboot application.yml file before SpringBootApplication is initialized application.yml 和 @value - application.yml and @value 解析 application.yml - Parsing application.yml 测试模块无法加载其 application.yml 文件 - test module cannot load its application.yml file Spring引导单元测试不适用于application.yml - Spring boot unit test not work with application.yml 春季测试类不读取application.yml文件 - Spring test classes not reading application.yml file "'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test 无法从黄瓜测试中读取 application.yml - Unable to read the application.yml from cucumber test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM