简体   繁体   中英

Test is not read a value of a properties file

I'm developing an app (Java) and there is a test that it fails me. This mistake is because I want to read a value that it's in a properties file, but this value is null however If I execute my app skipping the tests, it works ok, it reads the value of the properties file without problem, Do I have to do something different to read this property in test package?

My test class

@Test
public void shouldBeControllerInitial() {

   String index = initController.init();

   assertEquals("Should be same to the index, it's main page", INDEX,
                index);
}

My controller class

@Controller
@RequestMapping(value = "/")
public class InitController {

    @Value("${path.index}")
    private String index;

    @RequestMapping(method = RequestMethod.GET)
    public String init() {

        return index;
    }

}

My config class

@Configuration
@PropertySource("classpath:/path.properties")
public class Config {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }

error message

java.lang.AssertionError: Should be same to the index, it's main page
expected:<index> but was:<null> ...

Can anyone help ?

As we have only test's body and not a whole source file, so I can only suggest:

  • check that in test you are injecting initController and didn't creating it with new
  • ensure that test uses your Config (you should have annotation like this: @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM