Integration test with DBUnit IO Exception Java -
i write integration test on springframework controller have issues data base. not undertand why dbunit can not locate schema.sql file. tried use absolute path did not work too.
controllertest
@runwith(springjunit4classrunner.class) @contextconfiguration @testexecutionlisteners({ dependencyinjectiontestexecutionlistener.class, dirtiescontexttestexecutionlistener.class, transactionaltestexecutionlistener.class, dbunittestexecutionlistener.class }) @databasesetup(value = "login.xml") public class logincontrollertest { private mockmvc mockmvc; private static final string jdbc_driver = org.h2.driver.class.getname(); private static final string jdbc_url = "jdbc:h2:mem:test;db_close_delay=-1"; private static final string schema_path = "schema.sql"; private static final string user = "sa"; private static final string password = ""; @beforeclass public static void createschema() throws exception { runscript.execute(jdbc_url, user, password, schema_path, null, false); } @before public void setup() throws exception { mockmvc = mockmvcbuilders.xmlconfigsetup("logincontrollertest-context.xml").build(); idataset dataset = readdataset(); cleanlyinsert(dataset); } private idataset readdataset() throws exception { return new flatxmldatasetbuilder().build(new file("login.xml")); } private void cleanlyinsert(idataset dataset) throws exception { idatabasetester databasetester = new jdbcdatabasetester(jdbc_driver, jdbc_url, user, password); databasetester.setsetupoperation(databaseoperation.clean_insert); databasetester.setdataset(dataset); databasetester.onsetup(); } @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("od", 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()))); } }
logincontrollertest-context
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <tx:annotation-driven transaction-manager="transactionmanager" /> <context:component-scan base-package="com.profiles.controller.test" /> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="org.h2.jdbcx.jdbcdatasource" /> <property name="url" value="jdbc:hsqldb:mem:login" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> </beans>
stack trace
org.h2.message.dbexception: io exception: "java.io.filenotfoundexception: schema.sql (the system cannot find file specified)"; "schema.sql" [90031-191] @ org.h2.message.dbexception.get(dbexception.java:168) @ org.h2.message.dbexception.convertioexception(dbexception.java:330) @ org.h2.tools.runscript.process(runscript.java:333) @ org.h2.tools.runscript.execute(runscript.java:303) @ com.profiles.controller.test.logincontrollertest.createschema(logincontrollertest.java:54) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.junit.internal.runners.classroadie.runbefores(classroadie.java:49) @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:36) @ org.junit.internal.runners.junit4classrunner.run(junit4classrunner.java:42) @ 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.h2.jdbc.jdbcsqlexception: io exception: "java.io.filenotfoundexception: schema.sql (the system cannot find file specified)"; "schema.sql" [90031-191] @ org.h2.message.dbexception.getjdbcsqlexception(dbexception.java:345) ... 18 more caused by: java.io.filenotfoundexception: schema.sql (the system cannot find file specified) @ java.io.fileinputstream.open0(native method) @ java.io.fileinputstream.open(fileinputstream.java:195) @ java.io.fileinputstream.<init>(fileinputstream.java:138) @ java.io.fileinputstream.<init>(fileinputstream.java:93) @ org.h2.store.fs.filepathdisk.newinputstream(filepathdisk.java:321) @ org.h2.store.fs.fileutils.newinputstream(fileutils.java:218) @ org.h2.tools.runscript.process(runscript.java:185) @ org.h2.tools.runscript.process(runscript.java:328) ... 15 more
solution
i have fix problem when move sql file folder under test source folder , set path - classpath:/sql/schema.sql
Comments
Post a Comment