java - How to Autowire in Junit Tests (General Newb Enquiry) -
this general question spring , creating application contexts.
i working on unit test web site application. website uses spring , hibernate , want test data database. not know how connect spring ioc container unit tests. know applicationcontext.xml lives, how access beans in unit tests?
here code:
package com.example.test; import junit.framework.assert; import org.hibernate.sessionfactory; import org.hibernate.classic.session; import org.junit.test; import org.springframework.beans.factory.annotation.autowired; public class sessionfactorytest { @autowired sessionfactory sessionfactory; @test public void testsessionfactoryautowiring() { session session = sessionfactory.getcurrentsession(); assert.assertequals(session.getclass(),session.class); } }
which generates error
testsuite: com.example.test.sessionfactorytest tests run: 1, failures: 0, errors: 1, time elapsed: 0.011 sec testcase: testsessionfactoryautowiring took 0.002 sec caused error null java.lang.nullpointerexception @ com.example.test.sessionfactorytest.testsessionfactoryautowiring(sessionfactorytest.java:19)
now question is, how access sessionfactory normal applicationcontex.xml? how access container? dont mind creating own, end of today love writing unit tests. wouldn't?
update: did add annotations according spring documentation test class
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = {"classpath:applicationcontext.xml"})
but file cannot found , not know how specify file relative test.
testsuite: com.example.test.sessionfactorytest tests run: 1, failures: 0, errors: 1, time elapsed: 0.045 sec testcase: testsessionfactoryautowiring took 0.035 sec caused error failed load applicationcontext java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:308) @ 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:220) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:301) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:303) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:240) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:180) caused by: org.springframework.beans.factory.beandefinitionstoreexception: ioexception parsing xml document class path resource [applicationcontext.xml]; nested exception java.io.filenotfoundexception: class path resource [applicationcontext.xml] cannot opened because not exist @ org.springframework.beans.factory.xml.xmlbeandefinitionreader.loadbeandefinitions(xmlbeandefinitionreader.java:341) @ org.springframework.beans.factory.xml.xmlbeandefinitionreader.loadbeandefinitions(xmlbeandefinitionreader.java:302) @ org.springframework.beans.factory.support.abstractbeandefinitionreader.loadbeandefinitions(abstractbeandefinitionreader.java:143) @ org.springframework.beans.factory.support.abstractbeandefinitionreader.loadbeandefinitions(abstractbeandefinitionreader.java:178) @ org.springframework.beans.factory.support.abstractbeandefinitionreader.loadbeandefinitions(abstractbeandefinitionreader.java:149) @ org.springframework.beans.factory.support.abstractbeandefinitionreader.loadbeandefinitions(abstractbeandefinitionreader.java:212) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:81) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:1) @ org.springframework.test.context.testcontext.loadapplicationcontext(testcontext.java:280) @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:304) caused by: java.io.filenotfoundexception: class path resource [applicationcontext.xml] cannot opened because not exist @ org.springframework.core.io.classpathresource.getinputstream(classpathresource.java:158) @ org.springframework.beans.factory.xml.xmlbeandefinitionreader.loadbeandefinitions(xmlbeandefinitionreader.java:328)
the structure of project looks like:
src/ web/web-inf/applicationcontext.xml test/java/com/example/sessionfactorytest.java
and built ant.
using classpath in locations not work because you're not using default location applicationcontext.xml
but work.
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "src/stuff web/web-inf/applicationcontext.xml" }) public class sessionfactorytest {
i suppose have in application context file also:
<context:component-scan base-package="com.example" /> <bean id="sessionfactory" class="com.example.sessionfactoryimpl" />
Comments
Post a Comment