【问题标题】:Spring Boot - Junit unable to load configuration properties in junit testSpring Boot - Junit 无法在 junit 测试中加载配置属性
【发布时间】:2019-04-26 10:12:51
【问题描述】:

我有一个consumer.properties 文件,在src/main/resources 中有以下内容,以及一个将文件内容加载并存储到类成员变量中的配置类:

//consumer.propertiessrc/main/resources中的文件:

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

//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 + "]";
    }
}

我还有一个 ConfigsService 类,它自动连接 ConsumerConfig 类来检索各个属性:

@Component
public class ConfigsService {

    @Autowired
    ConsumerConfig consumerConfig;

    public ConsumerConfig getConsumerConfig() {
        return consumerConfig;
    }

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

    public ConsumerConfig getConfig() {
        return consumerConfig;
    }
}

在运行 ConfigsService 的方法时,属性加载得很好。问题出在单元测试中,其中调用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);
    }
}

【问题讨论】:

  • 嗨,你是用 spring runner 运行这个,否则你不会在测试期间启动 spring 实例?

标签: java spring spring-boot properties


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    由于模拟对象,您得到空值。我会建议使用带有上下文配置的 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());
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 2017-02-12
      • 2023-04-10
      • 2015-08-20
      • 1970-01-01
      • 2012-06-18
      • 2020-02-09
      • 1970-01-01
      相关资源
      最近更新 更多