spring-mock + dbutil 用来测试数据库操作

需要的jar 1.spring-mock.jar 2.dbunit-2.2.jar 3.junit.jar[@more@]

大概流程:
1.用dbunit创建初始的测试数据。
2.用spring-mock 维护测试过程中的数据会滚,这样可以保证测试后数据库保持原状态。
3.用junit架构测试。
4.用dbunit销毁初始测试数据。

详细过程:
创建测试数据:

dbunit采用配置文件方式,将测试数据放入.xml文件中,形式非常简单:



列名1 = 值1
列名2 = 值2
列名3 = 值3
列名4 = 值4
……
……
/>


程序将上面数据加载到数据库
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

protected IDataSet getDataSet() throws Exception {
File f = new File(".");
String filePath = f.getAbsolutePath().substring(0,f.getAbsolutePath().length()-1)+"confdbdataSet4mock.xml";
return new FlatXmlDataSet(new FileInputStream(new File(filePath)));
}

初始数据:

databaseTester = new JdbcDatabaseTester("com.mysql.jdbc.Driver",
"jdbc:mysql://127.0.0.1:3306/datetable", "sa", "sa");
IDataSet dataSet = getDataSet();//导入初始数据
databaseTester.setDataSet(dataSet);
databaseTester.onSetup();

销毁数据:
databaseTester.setTearDownOperation(DatabaseOperation.DELETE);
databaseTester.onTearDown();

下面看看spring-mock:
1.封装了junit的testcase:
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
这个类起到的作用:
a.继承testcase
b.读取spring-bean.xml文件中的事务处理,将监听所有事务,然后在测试结束后,进行事务会滚,这样保证在测试过程中产生的数据在结束后不会保存到数据库中。
c.override getConfigLocations()这个函数,将spring读取bean操作进行封装。

public abstract class SpringDAOTestCase extends AbstractTransactionalDataSourceSpringContextTests {
protected String[] getConfigLocations() {
File f = new File(".");
String filePath ="file:" + f.getAbsolutePath().substring(0,f.getAbsolutePath().length()-1)+"confdbspring-context-db.xml";
return new String[] { filePath };
}
}

public class DaoTest4Mock extends SpringDAOTestCase {
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
/*
dbunit 初始化数据
*/
}

protected void onTearDownInTransaction() {
/*
dbunit 释放数据
*/
}

public void testInsert() {
………………
}

public void testUpdate() {
………………
}

public void testDelete() {
………………
}

public void testSelect() {
………………
}
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9879276/viewspace-996348/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9879276/viewspace-996348/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework.mock.web.DelegatingServletInputStream.class org.springframework.mock.web.DelegatingServletOutputStream.class org.springframework.mock.web.HeaderValueHolder.class org.springframework.mock.web.MockExpressionEvaluator.class org.springframework.mock.web.MockFilterChain.class org.springframework.mock.web.MockFilterConfig.class org.springframework.mock.web.MockHttpServletRequest.class org.springframework.mock.web.MockHttpServletResponse.class org.springframework.mock.web.MockHttpSession.class org.springframework.mock.web.MockMultipartFile.class org.springframework.mock.web.MockMultipartHttpServletRequest.class org.springframework.mock.web.MockPageContext.class org.springframework.mock.web.MockRequestDispatcher.class org.springframework.mock.web.MockServletConfig.class org.springframework.mock.web.MockServletContext.class org.springframework.mock.web.PassThroughFilterChain.class org.springframework.mock.web.portlet.MockActionRequest.class org.springframework.mock.web.portlet.MockActionResponse.class org.springframework.mock.web.portlet.MockMultipartActionRequest.class org.springframework.mock.web.portlet.MockPortalContext.class org.springframework.mock.web.portlet.MockPortletConfig.class org.springframework.mock.web.portlet.MockPortletContext.class org.springframework.mock.web.portlet.MockPortletPreferences.class org.springframework.mock.web.portlet.MockPortletRequest.class org.springframework.mock.web.portlet.MockPortletRequestDispatcher.class org.springframework.mock.web.portlet.MockPortletResponse.class org.springframework.mock.web.portlet.MockPortletSession.class org.springframework.mock.web.portlet.MockPortletURL.class org.springframework.mock.web.portlet.MockRenderRequest.class org.springframework.mock.web.portlet.MockRenderResponse.class org.springframework.test.AbstractDependencyInjectionSpringContextTests.class org.springframework.test.AbstractSingleSpringContextTests.class org.springframework.test.AbstractSpringContextTests.class org.springframework.test.AbstractTransactionalDataSourceSpringContextTests.class org.springframework.test.AbstractTransactionalSpringContextTests.class org.springframework.test.AssertThrows.class org.springframework.test.ConditionalTestCase.class org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.class org.springframework.test.annotation.DirtiesContext.class org.springframework.test.annotation.ExpectedException.class org.springframework.test.annotation.IfProfileValue.class org.springframework.test.annotation.NotTransactional.class org.springframework.test.annotation.ProfileValueSource.class org.springframework.test.annotation.Repeat.class org.springframework.test.annotation.SystemProfileValueSource.class org.springframework.test.annotation.Timed.class org.springframework.test.jpa.AbstractAspectjJpaTests.class org.springframework.test.jpa.AbstractJpaTests.class org.springframework.test.jpa.OrmXmlOverridingShadowingClassLoader.class org.springframework.test.web.AbstractModelAndViewTests.class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值