在spring测试中加载文件
我在项目中的文件夹结构是:
./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文件夹加载文件?
转载请注明出处:http://www.tstxxqczl.com/article/20230526/2061121.html