我正在开发一个应用程序(Java)并且有一个测试它让我失望。这个错误是因为我想要读取它在属性文件中的值,但是这个值为null但是如果我执行我的应用程序跳过测试,它可以正常工作,它会毫无问题地读取属性文件的值,我有吗做一些不同的事情来在测试包中读取这个属性?
我的考试类
@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> ...
有人可以帮忙吗?
答案 0 :(得分:0)
因为我们只有测试的主体而不是整个源文件,所以我只能建议:
initController
并且未使用new
创建Config
(您应该注释如下:@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class
)