ConfigurationProperties未加载ContextConfiguration

时间:2018-03-27 13:54:32

标签: spring-boot automated-tests

我正在尝试为转换为spring boot的项目运行测试。测试最初看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContextTestFnx.xml"})
@TestExecutionListeners({
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class
})

虽然我没有编写测试,但xml正在定义Derby数据源和组件扫描,而不是真正的数据源。我将其更改为此,因为我希望它能找到我的application-test.properties。

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContextTestFnx.xml"})

@SpringBootTest(properties = "spring.cloud.config.enabled=false")
@ActiveProfiles("test")


@TestExecutionListeners({
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class
})

我遇到的问题是没有填充关键配置属性对象。我相信@ContextConfiguration正常的弹簧启动初始化。

我尝试将其添加到XML但它无法正常工作

<bean class="com.myco.MailConfig"/>

配置对象非常基本:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@RefreshScope
@Configuration
@ConfigurationProperties("mail")
public class MailConfig {

    private String                  baseURL;
    private String                  Subject;
    private String                  to;
    private String                  from;
    private String                  enabled;

    <public getters and setters omitted>

}

有没有办法让我的配置对象填充?

0 个答案:

没有答案