【发布时间】:2017-06-04 19:08:46
【问题描述】:
我在项目中的文件夹结构是:
./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
在 ContextConfig 中,我从 assets 文件夹中加载一些属性文件,如下所示:
@PropertySources({
@PropertySource("file:assets/strings.properties")
})
public class ContextConfig {
/*some code...*/
}
请注意,我使用的不是classpath:,而是file:
在 TestContextConfig 中,我正在导入 ContextConfig,并且我正在像这样激活嵌入式 mongo:
@Configuration
@Import({
ContextConfig.class,
MongoAutoConfiguration.class
})
public class TestContextConfig {
}
当我尝试在我的测试类中使用它时:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContextConfig.class)
public class ProofOfConcept {
/*some code...*/
}
我得到 FileNotFoundException 因为它找不到属性文件,但是当应用程序正常启动(不是在测试中)时,一切正常。 我得到的例外:
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)
如何在测试中从 assets 文件夹加载文件?
【问题讨论】:
标签: java spring spring-boot properties spring-test