在 Spring Boot 测试中获取配置 属性 class bean
Get Configuration Property class bean in SpringBoot Tests
我有一个 class 定义为自动从我的 sprintboot 应用程序中的 applicaton-*.properties 加载属性:
@Component
@ConfigurationProperties("my-app")
@EnableConfigurationProperties
@Data
public class MyAppProperties {
private String propertyX;
private String propertyY;
..
}
现在这是我的测试 class:
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {
@Autowired
private static RestTemplate restTemplate;
@Autowired
public static MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
在我的测试中,myAppProperties 始终为空。我如何在我的测试中获得这个实例?
将 @EnableConfigurationProperties(value = MyAppProperties.class)
添加到您的测试中 class:
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableConfigurationProperties(value = MyAppProperties.class)
public class MessageProcessorApplicationTests {
@Autowired
private RestTemplate restTemplate;
@Autowired
public MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
用 @RunWith(SpringRunner.class)
注释 MessageProcessorApplicationTests
以自动加载 application.properties 文件
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {
@Autowired
private static RestTemplate restTemplate;
@Autowired
public static MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX());
}
我有一个 class 定义为自动从我的 sprintboot 应用程序中的 applicaton-*.properties 加载属性:
@Component
@ConfigurationProperties("my-app")
@EnableConfigurationProperties
@Data
public class MyAppProperties {
private String propertyX;
private String propertyY;
..
}
现在这是我的测试 class:
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {
@Autowired
private static RestTemplate restTemplate;
@Autowired
public static MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
在我的测试中,myAppProperties 始终为空。我如何在我的测试中获得这个实例?
将 @EnableConfigurationProperties(value = MyAppProperties.class)
添加到您的测试中 class:
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableConfigurationProperties(value = MyAppProperties.class)
public class MessageProcessorApplicationTests {
@Autowired
private RestTemplate restTemplate;
@Autowired
public MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX()) //myAppProperties is null!! why?
}
用 @RunWith(SpringRunner.class)
注释 MessageProcessorApplicationTests
以自动加载 application.properties 文件
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MessageProcessorApplicationTests {
@Autowired
private static RestTemplate restTemplate;
@Autowired
public static MyAppProperties myAppProperties;
@Test
public void testSomething(){
doSomeSeetup(myAppProperties.getPropertyX());
}