简体   繁体   English

如何将application.properties加载到测试类中以获取spring.profiles.active值?

[英]How I can load application.properties into test class for getting the spring.profiles.active value?

I'm writing integration test which should works only with the specific profile. 我正在编写仅适用于特定配置文件的集成测试。 The problem is that I should get profile name from spring.profiles.active value in application.properties file. 问题是我应该从application.properties文件中的spring.profiles.active值获取配置文件名称。 But I always get null due to real value from application.properties. 但是由于application.properties的实际价值,我总是得到null。

I've created for main method which explicitly put value into System.properties: 我为主要方法创建了方法,该方法明确将值放入System.properties中:

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
}
    @Autowired
   public MyApplication(Environment environment) {
      String property = "spring.profiles.active";
      System.getProperties().setProperty(property, environment.getProperty(property));
   }

But the value still is null. 但是该值仍然为null。 In the test class I use following annotations: 在测试类中,我使用以下注释:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyApplication.class)
@SpringBootTest(classes=MyApplication.class)

For loading context from the main class where I manually set the necessary value. 为了从主类加载上下文,我手动设置了必要的值。 Also tried use @TestPropertySource("classpath:application.properties") But nothing help. 也尝试使用@TestPropertySource("classpath:application.properties")但没有帮助。 And I can't hardcode the value. 而且我无法对值进行硬编码。 It's should be get only from application.properties 应该只能从application.properties获取

UPDATE It was my mistake. 更新这是我的错误。 I've tried getValue from static method :/ Maybe someone know how I can disable class test by use this value? 我已经尝试过从静态方法获取值:/也许有人知道我可以通过使用该值禁用类测试吗? The @IfProfileValue still return null and therefore doesn't fit. @IfProfileValue仍返回null,因此不适合。

UPDATE I've realized that disabling is useless and better just use appropriate config using @SpringBootTest(classes=AppropriateConfigClass.class) 更新我已经意识到禁用是没有用的,更好的方法是使用@SpringBootTest(classes = AppropriateConfigClass.class)使用适当的配置

I'll choose gargkshitiz answer as solved, because he was the only one who tried to help. 我将选择“ argskshitiz”作为解决方案,因为他是唯一尝试提供帮助的人。

You are halfway there. 你在那儿 @TestPropertySource reads from src/test/resources . @TestPropertySourcesrc/test/resources读取。 Add src/test/resources/application.properties with overridden values and you should be done. 使用覆盖的值添加src/test/resources/application.properties ,您应该已完成。

Answer by @gargkshitiz is perfect ! @gargkshitiz的回答是完美的!

In case you have only property spring.profiles.active to be read then you can mention it following way(it will save us from maintaining properties file). 如果您只有属性spring.profiles.active要读取,则可以按照以下方式提及它(这将使我们免于维护属性文件)。

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
                value={"spring.profiles.active=yourActiveProfileName"})

Also you can specify multiple properties, 您还可以指定多个属性,

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
                value={"spring.profiles.active=yourActiveProfileName",
                       "spring.some.other.property=propertyValue"})

You can drop webEnvironment = WebEnvironment.RANDOM_PORT if you don't need it. 如果不需要,可以删除webEnvironment = WebEnvironment.RANDOM_PORT

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "无法解析值“classpath:\/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”" - Could not resolve placeholder 'spring.profiles.active' in value "classpath:/ldap-${spring.profiles.active}.properties" 如何在 log4j2.xml 中获得 spring.profiles.active 值 - How can I get spring.profiles.active value in log4j2.xml 通过 spring.profiles.active 覆盖默认应用程序属性 - Override default application properties by spring.profiles.active spring.profiles.active 在springboot应用程序中不起作用 - spring.profiles.active is not working in springboot application 如何将 application.properties 与 application-default.properties 合并并包含在 Spring Boot >2.4 的其他测试配置文件中 - How to merge application.properties with application-default.properties and include in other test profiles with Spring Boot >2.4 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file spring.profiles.active 不兑现 - spring.profiles.active not honoured 春季启动:spring.profiles.active = dev / test / prod - Spring Boot: spring.profiles.active=dev/test/prod 从位置“类路径资源 [application-dev.yml]”导入的属性“spring.profiles.active”无效 - Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid Spring 引导获取 static 块中的 spring.profiles.active 值 - Spring Boot get spring.profiles.active value in a static block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM