简体   繁体   English

Spring Boot - Junit 无法在 junit 测试中加载配置属性

[英]Spring Boot - Junit unable to load configuration properties in junit test

I have a consumer.properties file with the following contents in src/main/resources , and an accompanying Configuration class that loads and stores the file's contents into class member variables:我在src/main/resources有一个包含以下内容的consumer.properties文件,以及一个随附的 Configuration 类,该类将文件的内容加载并存储到类成员变量中:

// consumer.properties file in src/main/resources : // src/main/resources consumer.properties文件:

com.training.consumer.hostname=myhost
com.training.consumer.username=myusername
com.training.consumer.password=mypassword

//ConsumerConfig.java //ConsumerConfig.java

@Configuration
@PropertySource(
        value= {"classpath:consumer.properties"}
        )
@ConfigurationProperties(prefix="com.training.consumer")
public class ConsumerConfig {

    private String hostname;
    private String username;
    private String password;

    public ConsumerConfig() { }

    public String getHostname() {
        return hostname;
    }
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ConsumerConfig [hostname=" + hostname + ", username=" + username + ", password=" + password + "]";
    }
}

I also have a ConfigsService class that autowires the ConsumerConfig class to retrieve the individual properties:我还有一个ConfigsService类,它自动连接ConsumerConfig类以检索各个属性:

@Component
public class ConfigsService {

    @Autowired
    ConsumerConfig consumerConfig;

    public ConsumerConfig getConsumerConfig() {
        return consumerConfig;
    }

    public void showConfig() {
        consumerConfig.toString();
    }

    public ConsumerConfig getConfig() {
        return consumerConfig;
    }
}

The properties are loaded up just fine when running the ConfigsService's methods.运行 ConfigsService 的方法时,这些属性加载得很好。 The problem is in the unit tests, where invoking configService.getConfig().getHostname() returns a null value -- even after having created a src/test/resources directory, and adding my consumer.properties file in it:问题出在单元测试中,其中调用configService.getConfig().getHostname()返回空值——即使在创建了src/test/resources目录并在其中添加了我的consumer.properties文件之后:

@TestPropertySource("classpath:consumer.properties")
public class ConfigsServiceTest {

    @Mock
    ConsumerConfig consumerConfig;

    @InjectMocks
    ConfigsService configService;


    @Before
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void someTest() {
        System.out.println(configService.getConfig().getHostname()); //outputs null here -- wth!
        Assert.assertTrue(true);
    }
}

you are getting null values because of the mock object.由于模拟对象,您将获得空值。 I will suggest using spring runner with context configuration which will load properties in the config class and create the bean.我会建议使用带有上下文配置的 spring runner,它将加载配置类中的属性并创建 bean。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConsumerConfig.class, ConfigsService.class})
@TestPropertySource(locations = "classpath:consumer.properties")
public class ConfigsServiceTest {

  @Autowired
  private ConfigsService configsService;

  @Test
  public void someTest() {
    Assert.assertNotNull(configService.getConfig().getHostname());
  }
}

该错误可能是因为在 ConfigsServiceTest 类中,您正在确定您的 ConsumerConfig 是一个 Mock,这就是为什么如果您删除它应该可以工作的代码,它不会加载您的配置

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM