Saturday, 15 January 2011

java - Junit 4 Spring - Failed to load ApplicationContext -



java - Junit 4 Spring - Failed to load ApplicationContext -

i using spring 3.1, hibernate 4 , added junit 4.8.2 create unit tests.

my folder construction is:

src/main/java -> sources src/main/resources -> messages.properties file src/test/java -> dao bundle containing officialholidaydaotest src/test/resources -> applicationcontext-test.xml

my applicationcontext.xml found under src/main/webapp/web-inf

i trying run next test:

@runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "file:src/test/resources/applicationcontext-test.xml" }) public class officialholidaydaotest extends abstracttransactionaljunit4springcontexttests {...}

which fails with:

------------------------------------------------------------------------------- test set: dao.officialholidaydaotest ------------------------------------------------------------------------------- tests run: 1, failures: 0, errors: 1, skipped: 0, time elapsed: 1.866 sec <<< failure! testaddofficialholiday(dao.officialholidaydaotest) time elapsed: 1.75 sec <<< error! java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:157) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:109) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:75) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:321) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:211) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:288) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:290) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:231) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:49) @ org.junit.runners.parentrunner$3.run(parentrunner.java:193) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:52) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:191) @ org.junit.runners.parentrunner.access$000(parentrunner.java:42) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:184) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.junit.runners.parentrunner.run(parentrunner.java:236) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:174) @ org.apache.maven.surefire.junit4.junit4testset.execute(junit4testset.java:53) @ org.apache.maven.surefire.junit4.junit4provider.executetestset(junit4provider.java:123) @ org.apache.maven.surefire.junit4.junit4provider.invoke(junit4provider.java:104) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.apache.maven.surefire.util.reflectionutils.invokemethodwitharray(reflectionutils.java:164) @ org.apache.maven.surefire.booter.providerfactory$providerproxy.invoke(providerfactory.java:110) @ org.apache.maven.surefire.booter.surefirestarter.invokeprovider(surefirestarter.java:175) @ org.apache.maven.surefire.booter.surefirestarter.runsuitesinprocesswhenforked(surefirestarter.java:107) @ org.apache.maven.surefire.booter.forkedbooter.main(forkedbooter.java:68) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'logincontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: protected org.springframework.security.authentication.authenticationmanager com.infobest.vms.authentication.logincontroller.authenticationmanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [org.springframework.security.authentication.authenticationmanager] found dependency: expected @ to the lowest degree 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true), @org.springframework.beans.factory.annotation.qualifier(value=authenticationmanager)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:287) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1106) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:517) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:294) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:225) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:585) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:913) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:464) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:103) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:1) @ org.springframework.test.context.support.delegatingsmartcontextloader.loadcontext(delegatingsmartcontextloader.java:228) @ org.springframework.test.context.testcontext.loadapplicationcontext(testcontext.java:124) @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:148) ... 30 more caused by: org.springframework.beans.factory.beancreationexception: not autowire field: protected org.springframework.security.authentication.authenticationmanager com.infobest.vms.authentication.logincontroller.authenticationmanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [org.springframework.security.authentication.authenticationmanager] found dependency: expected @ to the lowest degree 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true), @org.springframework.beans.factory.annotation.qualifier(value=authenticationmanager)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:506) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:284) ... 45 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [org.springframework.security.authentication.authenticationmanager] found dependency: expected @ to the lowest degree 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true), @org.springframework.beans.factory.annotation.qualifier(value=authenticationmanager)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:924) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:793) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:707) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:478) ... 47 more

i'm not sure why authenticationmanager mentioned, because bean declared in spring-security.xml not included in src/test/ folders or needed tests.

i tried using classpath in context configuration nothing. ideas? thanks.

the problem was, authenticationmanager not found because contents of spring-security.xml not nowadays in applicationcontext-test.xml. copied contents of spring-security applicationcontext-test.xml.

i still using file:src/test/resources/applicationcontext-test.xml , works now.

java spring hibernate dependency-injection junit

No comments:

Post a Comment