【发布时间】:2018-06-15 04:01:47
【问题描述】:
我正在尝试使用 junit 对 spring-boot 应用程序进行单元测试。我已将 application-test.properties 放在 src/test/resources 下。我有一个读取 application.properties 的 ApplicationConfiguration 类。
我的测试类是这样的
@RunWith(SpringRunner.class)
@SpringBootTest(classes=ApplicationConfiguration.class)
@TestPropertySource(locations = "classpath:application-test.properties")
@ActiveProfiles("test")
public class TestBuilders {
@Autowired
private ApplicationConfiguration properties;
当我尝试读取属性时,它始终为空。
我的 ApplicationConfiguration 类看起来像这样
@Configuration
@ConfigurationProperties
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value="file:config.properties", ignoreResourceNotFound =
true)})
public class ApplicationConfiguration{
private xxxxx;
private yyyyy;
我尝试了在谷歌上找到的所有可能的方法.. 没有运气。请帮忙! 提前致谢。
【问题讨论】:
-
你能显示这个类ApplicationConfiguration
-
我刚刚编辑了我的问题以添加 ApplicationConfiguration.. 当我运行应用程序时,它正在获取属性。问题仅在我运行测试用例时出现。
-
属性对象是 null 还是 xxxxx 并且 yyyyy 是 null?
-
您是否使用 spring-boot 初始化程序创建了这个项目?
-
@pvpkiran 属性对象不为空...只有字段为空
标签: spring-boot spring-boot-test application.properties