junit - Fails to load Spring application context in tests with SpringJUnit4ClassRunner -


i can't figure out why spring can't load application context in test class defined follows:

    @runwith(springjunit4classrunner.class)     @contextconfiguration(locations={"/springmvc-config/maincontroller-servlet.xml"})     public class diagnosticscontrollertest {      @mock     diagnosticsservice diagnosticsservice;      private diagnosticscontroller diagnosticscontroller;      private mockmvc mockmvc;      @before     public void setup() {         mockitoannotations.initmocks(this);         diagnosticscontroller = new diagnosticscontroller();         diagnosticscontroller.setservice(diagnosticsservice);         this.mockmvc = mockmvcbuilders.standalonesetup(diagnosticscontroller).build();     }      @test     public void shouldrun() {         asserttrue(1 == 1);     } } 

the file 'maincontroller-servlet.xml' in "myproject\src\main\webapp\web-inf\springmvc-config\maincontroller-servlet.xml"

the error is:

java.lang.illegalstateexception: failed load applicationcontext     @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:124)     @ org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:83)     @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:117)     @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:83)     @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:228)     @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:230)     @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:289)     @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12)     @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:291)     @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:249)     @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:89)     @ org.junit.runners.parentrunner$3.run(parentrunner.java:290)     @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71)     @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288)     @ org.junit.runners.parentrunner.access$000(parentrunner.java:58)     @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268)     @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61)     @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70)     @ org.junit.runners.parentrunner.run(parentrunner.java:363)     @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:193)     @ org.junit.runner.junitcore.run(junitcore.java:137)     @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:74)     @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:211)     @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:67)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:134) caused by: org.springframework.beans.factory.beandefinitionstoreexception: ioexception parsing xml document class path resource [springmvc-config/maincontroller-servlet.xml]; nested exception java.io.filenotfoundexception: class path resource [springmvc-config/maincontroller-servlet.xml] cannot opened because not exist 

i tried change locations context configuration follows:

@runwith(springjunit4classrunner.class) @contextconfiguration("file:src/main/webapp/web-inf/springmvc-config/maincontroller-servlet.xml") public class diagnosticscontrollertest { ... } 

src/main/webapp not on classpath, hence can't find file.

see spring @contextconfiguration how put right location xml explanation of why it's bad put spring configuration webapp.

the solution move configuration src/main/resources, , tweak you're loading spring config files on classpath them. if you're using web.xml file, should simple prefixing them classpath:.

there other solutions, feel that's best option in long-term.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -