-1

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 ?

amdixon
  • 3,814
  • 8
  • 25
  • 34
Celer
  • 95
  • 7
  • Your question is unclear, but you can set up you environment to use a specific property file for unit testing only: http://stackoverflow.com/questions/24231773/specifying-a-custom-log4j-properties-file-for-all-of-junit-tests-run-from-eclips/24315819#24315819 – Raedwald Jun 18 '15 at 11:50
  • I added a properties file inside test package but it doesn't work – Celer Jun 18 '15 at 12:01

1 Answers1

0

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)
Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
  • Thanks you for your answer. About InitController I have it injecting with @InjectMock I have put inside my Config class, what you have put (`@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class)`) but it continues having mistake, my test is in red jejeje. – Celer Jun 19 '15 at 07:37
  • Most likely problem lies because you're using `@InjectMocks` and instead of real object from Spring context you're getting mock that just does nothing and returns `null` as return value of each methods. It's just assumption. Please, provide source of test class, may be I or other people will be able to help. – Slava Semushin Jun 19 '15 at 09:42