in Projekt Meine Ordnerstruktur ist:Laden von Dateien im Frühjahr Tests
./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 ich einige Eigenschaften Dateien laden von Assets wie diesen Ordner:
@PropertySources({
@PropertySource("file:assets/strings.properties")
})
public class ContextConfig {
/*some code...*/
}
Beachten Sie, dass ich nicht classpath:
mit aber file:
In TestContextConfig importiere ich ContextConfig und auch ich embedded Mongo wie folgt aktivieren:
@Configuration
@Import({
ContextConfig.class,
MongoAutoConfiguration.class
})
public class TestContextConfig {
}
Wenn ich versuche, es in meinem Test-Klasse zu verwenden:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContextConfig.class)
public class ProofOfConcept {
/*some code...*/
}
ich FileNotFoundException, weil es nicht Properties-Datei finden kann, aber wenn die Anwendung gestartet wird normalerweise (nicht in Tests) alles gut funktioniert. Ausnahme erhalte ich:
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)
Wie kann ich Dateien von Assets Ordner in Tests laden?
Froh, dass es Ihr Problem gelöst hat! – asg