[英]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
. @TestPropertySource
从src/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.