java测试不读取属性文件的值
我正在开发一个应用程序(Java),有一个测试显示它失败了。这个错误是因为我想读取属性文件中的一个值,但是这个值是空的,但是如果我执行我的应用程序跳过测试,它工作正常,它读取属性文件的值没有问题,我需要做一些不同的事情来读取测试包中的这个属性吗
我的测试课
@Test
public void shouldBeControllerInitial() {
String index = initController.init();
assertEquals("Should be same to the index, it's main page", INDEX,
index);
}
我的控制器类
@Controller
@RequestMapping(value = "/")
public class InitController {
@Value("${path.index}")
private String index;
@RequestMapping(method = RequestMethod.GET)
public String init() {
return index;
}
}
我的配置类
@Configuration
@PropertySource("classpath:/path.properties")
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
错误消息
java.lang.AssertionError: Should be same to the index, it's main page
expected:<index> but was:<null> ...
有人能帮忙吗
# 1 楼答案
由于我们只有测试的主体,而没有完整的源文件,所以我只能建议:
initController
,并且没有使用new
创建它Config
(应该有如下注释:@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class
)