2017-07-19 33 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更新所有引用。

相关问题