2017-07-19 34 views
2

這是一個非常奇怪的行爲。我有一個可以正常工作的Spring啓動應用程序。由於幾天(我剛換的應用程序到另一個混帳回購協議,如果這意味着什麼),我每次用的IntelliJ運行測試時我武功的錯誤:在IntelliJ社區2017.1中運行Spring啓動測試時出現錯誤。

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

所以說,它無法找到測試的.properties文件我在這裏聲明:

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

的文件是在類路徑中(src /主/ JAVA/COM/myCompany的/ myproject的/ test.properties)

現在最離奇的一部分。如果我用Eclipse(實際上是Sprig Tool Suite)打開項目並運行測試,它們就可以工作。然後我再次對IntelliJ運行測試,並且......他們工作!

因此,當我打開並刷新項目時,Eclipse/STS會進行一些更改,但我不知道它是什麼。

+0

該文件必須位於src/main/resources而不是src/main/java中,這適用於所有非java資源。 –

+0

如果該文件專用於測試,它應該在'src/test/resources'中。您還可以嘗試使用'classpath:test.properties'更改位置值。 [查看更多詳情](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-property-sources)。 – Thoomas

+0

如果我將文件從'src/main/java/com/mycompany/myproject'移走,則不再有效。我猜是因爲它將前綴'src/main/java/com/mycompany/myproject'作爲前綴。例如,我將'test.properties'移到了'src/main/resources'中,並且出現錯誤:'java.io.FileNotFoundException:無法打開類路徑資源[com/mycompany/myproject/test.properties],因爲它不存在'。添加'classpath:'也沒有幫助。 –

回答

0

解決方案是將test.properties文件拖到另一個文件夾,讓IntelliJ更新引用,然後再次將test.properties文件移回到根文件夾中,並再次讓IntelliJ更新所有引用。

相關問題