java - SpringFramework integration test Failed to load ApplicationContext -
i write integration test not know how fix illegalstateexception application context can not load. logincontrollertest2-context.xml in same package tested controller.
controllertest
@runwith(springjunit4classrunner.class) @webappconfiguration @contextconfiguration(locations = { "classpath:logincontrollertest2-context.xml" }) @testexecutionlisteners({ dependencyinjectiontestexecutionlistener.class, dirtiescontexttestexecutionlistener.class, transactionaltestexecutionlistener.class, dbunittestexecutionlistener.class }) @databasesetup("login.xml") public class logincontrollertest2 { private static final string jdbc_driver = "org.h2.driver"; private static final string jdbc_url = "jdbc:h2:mem:login;db_close_delay=-1"; private static final string user = "sa"; private static final string pass = ""; private static final string schema_file = "h2.sql"; private static final string dataset = "login.xml"; private mockmvc mockmvc; @resource private webapplicationcontext wac; @beforeclass public static void createschema() throws classnotfoundexception { class.forname(jdbc_driver); try { connection conn = datasource().getconnection(); inputstreamreader in = new inputstreamreader(logincontrollertest2.class.getresourceasstream(schema_file)); runscript.execute(conn, in); } catch (exception e) { // todo: handle exception } } @before public void loadtestdata() throws exception { // mockmvc = // mockmvcbuilders.xmlconfigsetup("classpath:logincontrollertest2-context.xml").build(); mockmvc = mockmvcbuilders.webapplicationcontextsetup(wac).build(); // mockmvc = // mockmvcbuilders.annotationconfigsetup(logincontrollertest2.class).build(); idataset ids = new flatxmldatasetbuilder().build(logincontrollertest2.class.getresourceasstream(dataset)); jdbcdatabasetester databasetester = new jdbcdatabasetester(jdbc_driver, jdbc_url, user, pass); databasetester.setsetupoperation(org.dbunit.operation.databaseoperation.clean_insert); databasetester.setdataset(ids); databasetester.onsetup(); } private static datasource datasource() { jdbcdatasource datasource = new jdbcdatasource(); datasource.seturl(jdbc_url); datasource.setuser(user); datasource.setpassword(pass); return datasource; } @test @expecteddatabase("login.xml") public void testshowform() throws exception { mockmvc.perform(get("/login")).andexpect(status().isok()).andexpect(view().name("/login")) .andexpect(forwardedurl("../../webcontent/j/login.jsp")) .andexpect(model().attribute("form", hasproperty("passportid", nullvalue()))) .andexpect(model().attribute("form", hasproperty("email", isemptyornullstring()))) .andexpect(model().attribute("form", hasproperty("username", isemptyornullstring()))) .andexpect(model().attribute("form", hasproperty("hostname", isemptyornullstring()))) .andexpect(model().attribute("form", hasproperty("pass", isemptyornullstring()))); } }
stacktrace
java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:94) @ org.springframework.test.context.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:72) @ 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:212) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:200) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:259) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:261) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:219) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:83) @ 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.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:68) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:163) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:86) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.jdbc.datasource.init.datasourceinitializer#0': invocation of init method failed; nested exception org.springframework.jdbc.datasource.init.cannotreadscriptexception: cannot read sql script servletcontext resource [/test/logincontroller/h2.sql]; nested exception java.io.filenotfoundexception: not open servletcontext resource [/test/logincontroller/h2.sql] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1574) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:539) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:755) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) @ org.springframework.test.context.web.abstractgenericwebcontextloader.loadcontext(abstractgenericwebcontextloader.java:133) @ org.springframework.test.context.web.abstractgenericwebcontextloader.loadcontext(abstractgenericwebcontextloader.java:60) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.delegateloading(abstractdelegatingsmartcontextloader.java:109) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.loadcontext(abstractdelegatingsmartcontextloader.java:261) @ org.springframework.test.context.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:68) @ org.springframework.test.context.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:86) ... 26 more caused by: org.springframework.jdbc.datasource.init.cannotreadscriptexception: cannot read sql script servletcontext resource [/test/logincontroller/h2.sql]; nested exception java.io.filenotfoundexception: not open servletcontext resource [/test/logincontroller/h2.sql] @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:437) @ org.springframework.jdbc.datasource.init.resourcedatabasepopulator.populate(resourcedatabasepopulator.java:229) @ org.springframework.jdbc.datasource.init.compositedatabasepopulator.populate(compositedatabasepopulator.java:60) @ org.springframework.jdbc.datasource.init.databasepopulatorutils.execute(databasepopulatorutils.java:48) @ org.springframework.jdbc.datasource.init.datasourceinitializer.execute(datasourceinitializer.java:108) @ org.springframework.jdbc.datasource.init.datasourceinitializer.afterpropertiesset(datasourceinitializer.java:93) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1633) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1570) ... 41 more caused by: java.io.filenotfoundexception: not open servletcontext resource [/test/logincontroller/h2.sql] @ org.springframework.web.context.support.servletcontextresource.getinputstream(servletcontextresource.java:141) @ org.springframework.core.io.support.encodedresource.getreader(encodedresource.java:138) @ org.springframework.jdbc.datasource.init.scriptutils.readscript(scriptutils.java:274) @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:434) ... 48 more
xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="test.logincontroller" /> <context:annotation-config /> <mvc:annotation-driven /> <context:spring-configured /> <!-- <tx:annotation-driven transaction-manager="transactionmanager" /> --> <bean id="datasource" class="org.springframework.jdbc.datasource.lazyconnectiondatasourceproxy"> <constructor-arg> <bean class="org.springframework.jdbc.datasource.simpledriverdatasource"> <property name="driverclass" value="org.h2.driver" /> <property name="url" value="jdbc:h2:mem:login;db_close_delay=-1;trace_level_system_out=2;trace_level_file=4" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> </constructor-arg> </bean> <jdbc:initialize-database data-source="datasource"> <jdbc:script location="/test/logincontroller/h2.sql" /> </jdbc:initialize-database> </beans>
somewhere in code, attempting lookup /test/logincontroller/h2.sql
servletcontext.
i'm not familiar mockmvc i'd guess need tell test web root servletcontext.getresource(...)
can find file.
Comments
Post a Comment