intellij提示找不到配置文件
错误代码如下:
严重: Caught exception ] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$.runReflectiveCall(SpringJUnit4ClassRunner.java:) at org.junit.) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:) at org.junit.runners.ParentRunner$.run(ParentRunner.java:) at org.junit.runners.ParentRunner$.schedule(ParentRunner.java:) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:) at org.junit.runners.ParentRunner.access$(ParentRunner.java:) at org.junit.runners.ParentRunner$.evaluate(ParentRunner.java:) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:) at org.junit.runners.ParentRunner.run(ParentRunner.java:) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:) at org.junit.runner.JUnitCore.run(JUnitCore.java:) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:) at java.lang.reflect.Method.invoke(Method.java:) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:) Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test/spring-idol.xml]; nested exception is java.io.FileNotFoundException: class path resource [test/spring-idol.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
目录结构:
源代码:
@ContextConfiguration("spring-idol.xml") public class AspectTest { @Autowired ApplicationContext context; @Test public void audienceShouldApplaud() throws Exception { // ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-idol.xml"); Performer eddie = (Performer) context.getBean("eddie"); eddie.perform(); }
解决方案:
更改@ContextConfiguration注解内容为
@ContextConfiguration("classpath*:/spring-idol.xml")
或为
@ContextConfiguration("../spring-idol.xml")
或者
将配置文件放在resources/test目录下。