您的位置:首页 > 运维架构 > Linux

100个最佳Linux站点

2013-04-07 18:55 183 查看
今天复习了一下 造福无数人Spring2.0 参考手册中文翻译--第8章 测试,浓缩即精华的更新了SpringSide wiki中的--Spring下的单元测试要点

注意,文档还在不断修改中,建议直接看Wiki 上的最新版本

1. 需要Spring 依赖注入的测试

为了测试Spring管理下的Bean,可以自行构造BeanFactory,也可以继承于AbstractDependencyInjectionSpringContextTests,实现public String[] getConfigLocations()函数, 返回applicationContext文件路径的数组。

protectedString[] getConfigLocations() {
returnnewString[]{"classpath*:spring/*.xml",  "classpath*:spring/test/*.xml"};
}


并显式写一些需要注入的变量的setter函数。

tips1:此基类有一个applicationContext的成员变量,所以除了依靠setter注入外,还可以随时用applicationContext.getBean() 取出所需的bean。

tips2:注意此基类 默认是autowire by type的,所以如果context文件里有两个相同类型的Bean就会报错,可能需要在getConfigLocations()函数里,setAutowireMode(AUTOWIRE_BY_NAME);把它设回by name,或者取消setter函数,自行用applicationContext.getBean()来显式查找Bean。


2. Dao测试

AbstractTransactionalDataSourceSpringContextTests 继承于AbstractDependencyInjectionSpringContextTests,除了拥有上类的能力外,还管理了每个测试的事务,会在每个测试后默认回滚所有的操作。

深层解释:此类的实现其实依赖于Application Context中定义的 PlatformTransactionManager。由于使用了Autowrie by type,可以任意取名。

另依赖于Application Context中定义的DataSource,同样可以任意取名。

tips1:如果需要在测试后提交,需要setRollBack(false); 或者调用setComplete()

tips2:此基类还通过注入的DataSource创建了一个JDBCTemplate 变量,可以跑SQL帮忙核对Hibernate的结果,Spring将确保该查询在同一个事务内执行。为正常工作你需要告诉你的ORM工具'刷新'它的已改变内容,例如使用Hibernate Session 接口的 flush() 方法。

tips3:除了tips2以外,还有countRowsInTable(StringtableName),deleteFromTables(String[]names) ,executeSqlScript(StringsqlResourcePath, booleancontinueOnError)三个简便函数。


3. Controller测试

Controller测试一般要用MockObject 分离Service层,要copy WEB-INF/下的相关文件copy 到classpath,而且Controller不含太多的逻辑,所有测试controller有点吃力不讨好,建议直接用Selenium进行集成测试,见(Selenium测试概述)。


4.SpringSide里的测试

因为Spring默认的基类名字较长,SpringSide 在core 的org.springside.core.test 中重新继承了它们,并提供了默认读取所有按springside 文件存放规则存放的context 文件的getConfigLocations()函数。

默认读取所有context文件的getConfigLocations()函数对速度和测试的隔离化都有影响,可以在子类重新实现。不过自己重新一个个写相关context文件也好烦,而且其实在全lazy-load的情况下,速度也还可以接受。如何取舍要自己平衡了。

对于CRUD的测试,在helloworld示例里的变量名都作了泛化,可以快速copy到另一个测试里。

另外,留意resources/spring/test 下的文件,利用了Spring的PropertyOverrideConfigurer

,新设定测试时的ApplicatonContext 里各个Bean的属性如指定测试用的DataSource,详细用法见Spring配置要点

<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://wiki.springside.org.cn/display/springside/SpringUnitTest"
dc:identifier="http://wiki.springside.org.cn/display/springside/SpringUnitTest"
dc:title="SpringUnitTest"
trackback:ping="http://wiki.springside.org.cn/rpc/trackback/75" />
</rdf:RDF>
--><!--
Root decorator: all decisions about how a page is to be decorated via the
inline decoration begins here.
--><!--
Switch based upon the context. However, for now, just delegate to a decorator
identified directly by the context.
-->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: