2017-07-19 1 views
2

Dies ist ein sehr seltsames Verhalten. Ich habe eine Spring-Boot-Anwendung, die gut funktioniert. Seit ein paar Tagen (Ich wechselte nur die App auf eine andere Git Repo, wenn es etwas bedeutet) jedes Mal, wenn ich Tests mit IntelliJ betreibe ich den Fehler gest:Fehler beim Ausführen von Spring Boot Tests in IntelliJ Community 2017.1

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.IllegalStateException: Failed to add PropertySource to Environment at org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:198) at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:100) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 24 common frames omitted Caused by: java.io.FileNotFoundException: class path resource [com/mycompany/myproject/test.properties] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154) at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72) at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58) at org.springframework.core.io.support.ResourcePropertySource.(ResourcePropertySource.java:84) at org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment(TestPropertySourceUtils.java:194) ... 27 common frames omitted 09:54:42.723 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test class: context [[email protected] testClass = AudioVideoControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = AudioVideoControllerTest, locations = '{}', classes = '{class com.mycompany.myproject.myprojectApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:/com/mycompany/myproject/test.properties}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.sp[email protected]548e7350, org.springfr[email protected]77cd7a0, org.springframework.boot.test.json.DuplicateJsonObje[email protected]754ba872, org.[email protected]0, org.springframework.boot[email protected]0, org.springframework.boot.test.autocon[email protected]13805618], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null]. Process finished with exit code -1

So sagt es es den Test nicht finden .properties-Datei ich erkläre hier:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = MyApplication.class) 
@TestPropertySource(locations="test.properties") 
@SpringBootTest 
public class AudioVideoControllerTest {...} 

die Datei befindet sich im classpath (src/main/java/com/mycompany/myproject/test.properties)

Nun ist die seltsamste Teil. Wenn ich das Projekt mit Eclipse (Sprig Tool Suite) öffne und Tests starte, funktionieren sie. Dann starte ich nochmal Tests auf IntelliJ und ... sie funktionieren!

So sieht es aus wie Eclipse/STS einige Änderungen vornimmt, wenn ich das Projekt öffne und aktualisiere, aber ich frage mich, was es ist.

+0

Die Datei muss sich in 'src/main/resources' befinden, nicht' src/main/java', dies gilt für alle non-java-Ressourcen. –

+0

Wenn die Datei für Tests reserviert ist, sollte sie sich in 'src/test/resources' befinden. Sie können auch versuchen, den Standortwert mit 'classpath: test.properties' zu ändern. [Überprüfen Sie dies für weitere Details] (https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-property-sources). – Thoomas

+0

Wenn ich die Datei von 'src/main/java/com/mycompany/meinprojekt 'wegbewege, funktioniert es nicht mehr. Ich nehme an, weil es "src/main/java/com/mycompany/meinprojekt" als Präfix vorlegt. Zum Beispiel habe ich 'test.properties' in' src/main/resources' verschoben und erhalte den Fehler: 'java.io.FileNotFoundException: Klassenpfadressource [com/mycompany/myproject/test.properties] kann nicht geöffnet werden, weil dies der Fall ist existiert nicht. Das Hinzufügen von 'classpath:' hilft auch nicht. –

Antwort

0

Lösung zieht die Datei test.properties in einen anderen Ordner, lässt IntelliJ Verweise aktualisieren und verschiebt dann die Datei test.properties wieder in den Stammordner und lässt IntelliJ erneut alle Verweise aktualisieren.

Verwandte Themen