测试中的Spring覆盖非application.properties文件



我使用一个外部纯Java库,该库使用一个名为updater.properties的文件。我目前正在用@SpringBootTest编写一些Spring Boot集成测试。我目前遇到的问题是,我找不到任何选项来通过Spring传递/使用不同的updater.properties文件。有解决方案吗?还是需要更新库才能正常工作?updater.properties在我的资源文件夹中,由库内部解析。如果有任何帮助,我会很高兴的!

@SaltukKezer,是的,你可以。第一步创建一个类似的配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:/updater.properties")
public class UpdaterProperties{

@Autowired
Environment env;
public String getMyPropertyValue() {
return env.getProperty("your.property.name");
}

}

然后第二步,只需获取此类的一个实例并调用getters

最新更新