JUnit test appears java.lang.IllegalStateException:Failed to load ApplicationContext

Source: Internet
Author: User
Tags apache tomcat

The course design requires the JUnit test, which I tested on the already well-done SSH project, and the test class code is as follows

Package Com.zhang.web.services;importStaticorg.junit.assert.*; import org.junit.after;import org.junit.before;import org.junit.test;import javax.annotation.Resource;  Import Org.junit.runner.RunWith; Import Org.springframework.test.context.junit4.abstractjunit4springcontexttests;import  Org.springframework.test.context.ContextConfiguration;  Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations="Classpath:spring-beans.xml")   Public classUserdaotest extends abstractjunit4springcontexttests{@Resource (name="Userdao") PrivateUserdao Userdao; Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao;} @Before Public voidsetUp () throws Exception {} @After Public voidTearDown () throws Exception {} @Test Public voidTestfinduser () {assertequals (Userdao.finduser (0),NULL);}}

The following error occurred while running the test

Severe:caught exception whileallowing Testexecutionlistener [Org.springframewor[email protected]a15856] to prepare test instance [email protected]]java.lang.illegalstateexception:failed to load Applicationcontextat Org.springframework.test.context.TestContext.getApplicationContext (Testcontext.java:157) at Org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies ( Dependencyinjectiontestexecutionlistener.java:109) at Org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance ( Dependencyinjectiontestexecutionlistener.java: the) at Org.springframework.test.context.TestContextManager.prepareTestInstance (Testcontextmanager.java:321) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest (Springjunit4classrunner.java :211) at org.springframework.test.context.junit4.springjunit4classrunner$1. Runreflectivecall (Springjunit4classrunner.java:288) at Org.junit.Internal. Runners.model.ReflectiveCallable.run (Reflectivecallable.java: the) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock ( Springjunit4classrunner.java:284) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (Springjunit4classrunner.java: 231) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (Springjunit4classrunner.java:  the) at org.junit.runners.parentrunner$3. Run (Parentrunner.java:231) at org.junit.runners.parentrunner$1. Schedule (Parentrunner.java: -) at Org.junit.runners.ParentRunner.runChildren (Parentrunner.java:229) at org.junit.runners.parentrunner.access$ the(Parentrunner.java: -) at org.junit.runners.parentrunner$2. Evaluate (Parentrunner.java:222) at Org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate ( Runbeforetestclasscallbacks.java: A) at Org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate ( Runaftertestclasscallbacks.java: in) at Org.junit.runners.ParentRunner.run (Parentrunner.java: -) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (Springjunit4classrunner.java:174) at ORG.ECLIPSE.JDT.Internal. Junit4.runner.JUnit4TestReference.run (Junit4testreference.java: -) at ORG.ECLIPSE.JDT.Internal. Junit.runner.TestExecution.run (Testexecution.java: -) at ORG.ECLIPSE.JDT.Internal. junit.runner.RemoteTestRunner.runTests (Remotetestrunner.java:467) at ORG.ECLIPSE.JDT.Internal. junit.runner.RemoteTestRunner.runTests (Remotetestrunner.java:683) at ORG.ECLIPSE.JDT.Internal. Junit.runner.RemoteTestRunner.run (Remotetestrunner.java:390) at ORG.ECLIPSE.JDT.Internal. Junit.runner.RemoteTestRunner.main (Remotetestrunner.java:197) caused by:org.springframework.beans.factory.BeanDefinitionStoreException:IOException parsing XML document  from classPath resource [spring-beans.xml]; Nested exception isJava.io.FileNotFoundException:classPath resource [Spring-beans.xml] cannot is opened because it does not exist

The workaround is to copy the Spring-beans.xml configuration file to the SRC directory.

Problem reason: I always think Eclipse's classpath includes Web-inf directory, not actually. We can open the. classpath file under the Web project with the following contents

<?xml version="1.0"encoding="UTF-8"? ><classpath><classpathentry kind="src"Path="src"/><classpathentry kind="Con"Path="Org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/jre7 "><attributes><attribute name="owner.project.facets"Value="Java"/></attributes></classpathentry><classpathentry kind="Con"Path="Org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimetarget/apache Tomcat v7.0"><attributes><attribute name="owner.project.facets"Value="Jst.web"/></attributes></classpathentry><classpathentry kind="Con"Path="Org.eclipse.jst.j2ee.internal.module.container"/><classpathentry kind="Con"Path="Org.eclipse.jdt.user_library/spring3"/><classpathentry kind="Con"Path="org.eclipse.jdt.user_library/struts"/><classpathentry kind="Con"Path="org.eclipse.jdt.user_library/hibernate"/><classpathentry kind="Con"Path="ORG.ECLIPSE.JDT.JUNIT.JUNIT_CONTAINER/4"/><classpathentry kind="Output"Path="build/classes"/></classpath>
In this, we do not see that the classpath contains the Web-inf directory. If we add <classpathentry kind= "src" path= "Webcontent/web-inf"/> and then restart Eclipse, you will notice that the above problem does not exist and the test class runs successfully.

JUnit test appears java.lang.IllegalStateException:Failed to load ApplicationContext

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.