Spring 启动测试 - 模拟配置
Spring boot Testing - Mock configurations
我想为我的 Spring 服务之一编写测试,服务中的方法之一是这样的:
private void loadConfig() throws JsonParseException, JsonMappingException, IOException {
// it loads config.json file and sets it in global variable
}
它从类路径加载 config.json
文件。
现在我的问题是:是否有可能以某种方式为测试定义它
它应该寻找 config-test.json 而不是 config.json ?假设我的类路径上有 config-test.json。
可能是方法上的一些注释,我没有从官方文档中找到太多帮助。
如有任何提示,我们将不胜感激。
您应该使用文件名作为方法参数,或者尝试对每个 ApplicationContext
生产和测试使用 @PropertySource
和 Spring Environment
。
我想为我的 Spring 服务之一编写测试,服务中的方法之一是这样的:
private void loadConfig() throws JsonParseException, JsonMappingException, IOException {
// it loads config.json file and sets it in global variable
}
它从类路径加载 config.json
文件。
现在我的问题是:是否有可能以某种方式为测试定义它
它应该寻找 config-test.json 而不是 config.json ?假设我的类路径上有 config-test.json。
可能是方法上的一些注释,我没有从官方文档中找到太多帮助。
如有任何提示,我们将不胜感激。
您应该使用文件名作为方法参数,或者尝试对每个 ApplicationContext
生产和测试使用 @PropertySource
和 Spring Environment
。