1

My folder structure in project is:

./assets/strings.properties
./module1/src/main/java/{some-packages}/ContextConfig.java
./module1/src/test/java/{some-packages}/TestContextConfig.java
./module1/pom.xml
./module2/pom.xml
./pom.xml

in ContextConfig I load some properties files from assets folder like this:

@PropertySources({
        @PropertySource("file:assets/strings.properties")
})
public class ContextConfig {
/*some code...*/
}

Notice that I'm not using classpath: but file:

In TestContextConfig I'm importing ContextConfig and also I'm activating embedded mongo like this:

@Configuration
@Import({
        ContextConfig.class,
        MongoAutoConfiguration.class
})
public class TestContextConfig {
}

When I try to use it in my test class:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContextConfig.class)
public class ProofOfConcept {
/*some code...*/
}

I get FileNotFoundException because it cannot find properties file, but when application is started normally (not in tests) everything is working well. Exception I get:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [{some-packages}.config.TestContextConfig]; nested exception is java.io.FileNotFoundException: assets\strings.properties (System nie może odnaleźć określonej ścieżki)
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:495)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:276)

How can I load files from assets folder in tests?

1 Answers1

1

Properties file -

./assets/strings.properties  

is appearing to be out of your test context. You will need to use the technique which will load external properties file for your application.

Please take a look at Spring application context external properties?. This should solve your problem. You can provide properties file location as a VM arguement or you can hard code in your test. Providing this property using VM argurments is a preferred way.

Community
  • 1
  • 1
asg
  • 2,248
  • 3
  • 18
  • 26