i trying use dbunit testing dao functionality. fo that, trying load dataset xml file. however, there points confusing me. let me post code first perspective.
public class mydaotest extends databasetestcase { private xmldataset loadeddataset; mydao dao; protected void setup() throws exception { dao = new courieruploadqueuemilestoneddaoimpl(); super.setup(); } @override protected idatabaseconnection getconnection() throws exception { class driverclass = class.forname("com.mysql.jdbc.driver"); connection jdbcconnection = drivermanager.getconnection("jdbc:mysql://localhost/test", "root", "root"); return new databaseconnection(jdbcconnection); } @override protected idataset getdataset() throws exception { fileinputstream in = new fileinputstream("dataset.xml"); loadeddataset = new xmldataset(in); return loadeddataset; } protected databaseoperation getsetupoperation() throws exception { return databaseoperation.clean_insert; } protected databaseoperation getteardownoperation() throws exception { return databaseoperation.none; } public void testchecklogindataloaded() throws exception { assertnotnull(loadeddataset); int rowcount = loadeddataset.gettable("person").getrowcount(); assertequals(2, rowcount); } }
the datset file dataset.xml
<?xml version='1.0' encoding='utf-8'?> <dataset> <person id="25" name="saurabh" phone="61458972564"/> <person id="21" name="saurabh" phone="61458972564"/> </dataset>
questions :
i exception "method threw
'org.dbunit.dataset.nosuchtableexception' exception."
when debug, don not see data being populated in"loadeddataset"
variable. isn't configuringdatabaseoperation.clean_insert operation in getsetupoperation()
required initilaize data set ?since, using xml file populating data set, why need give mysql connection details in getconnection() ? significance ? can use else here ? don't want accessing mysql database. behaviour want read xml file, write xml file in case of insert , update, , method completes revert dataset origiunal content.
i don't want using new operator create na instance of dao class. want use spring managed instance. tried doing
string[] configlocations = { "classpath:applicationcontext.xml" };
applicationcontext ctx = new classpathxmlapplicationcontext(configlocations); persondao = (persondao) ctx.getbean("persondao");
however. gave me
method threw 'org.springframework.beans.factory.beandefinitionstoreexception' exception. unexpected exception parsing xml document class path resource [applicationcontext.xml] detailmessage : org/springframework/core/type/annotatedtypemetadata
i find easiest way use dbunit spring set tests follows
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:applicationcontext-test.xml") @databasesetup("/dataset.xml") @testexecutionlisteners({ dependencyinjectiontestexecutionlistener.class, dirtiescontexttestexecutionlistener.class, transactionaltestexecutionlistener.class, dbunittestexecutionlistener.class }) public class sometest { @test public void test1() { ..... } }
you'll need following dependency in pom com.github.springtestdbunit.annotation.databasesetup class
<dependency> <groupid>com.github.springtestdbunit</groupid> <artifactid>spring-test-dbunit</artifactid> <version>1.2.1</version> <scope>test</scope> </dependency>
applicationcontext-test.xml is
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" 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.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="net.isban" /> <tx:annotation-driven /> <jpa:repositories base-package="net.isban.fmis.repository" /> <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="location" value="classpath:fmis-test.properties" /> </bean> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="org.h2.driver" /> <property name="url" value="jdbc:h2:mem:test;db_close_delay=-1" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="datasource" /> <property name="packagestoscan" value="net.isban.fmis.entity" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter" /> </property> <property name="jpaproperties"> <props> <prop key="hibernate.hbm2ddl.auto">create</prop> <prop key="hibernate.dialect">org.hibernate.dialect.h2dialect</prop> </props> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> </bean> <cache:annotation-driven /> <bean id="cachemanager" class="org.springframework.cache.ehcache.ehcachecachemanager"> <property name="cachemanager" ref="ehcache" /> </bean> <bean id="ehcache" class="org.springframework.cache.ehcache.ehcachemanagerfactorybean"> <property name="configlocation" value="classpath:ehcache-test.xml" /> </bean> </beans>
hope helps.
Comments
Post a Comment