Spring的项目测试的一些要点

Posted by eDWARD at 15:26

需要Spring 依赖注入的测试

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

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

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

Dao测试

  AbstractTransactionalDataSourceSpringContextTests 继承于AbstractDependencyInjectionSpringContextTests,除了拥有上类的能力外,还管理了每个测试的事务,会在每个测试后默认回滚所有的操作。
  深层解释,此类的实现其实依赖于Application Context中定义的PlatformTransactionManager。由于使用了autowire by type,可以任意取名。
  另依赖于Application Context中定义的DataSource,同样可以任意取名。
  tips1:如果需要在测试后提交,需要setRollBack(false); 或者调用setComplete()
  tips2:此基类还通过注入的DataSource创建了一个JDBCTemplate 变量,可以跑SQL帮忙核对Hibernate的结果,Spring将确保该查询在同一个事务内执行。为正常工作你需要告诉你的ORM工具’刷新’它的已改变内容,例如使用Hibernate Session 接口的 flush() 方法。
  tips3:除了tips2以外,还有countRowsInTable(String tableName),deleteFromTables(String[] names) ,executeSqlScript(String sqlResourcePath, boolean continueOnError)三个简便函数。

Controller测试

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

SpringSide里的测试

  因为Spring默认的基类名字较长,SpringSide 在core 的org.springside.core.test 中重新继承了它们,并提供了按springside的context文件存放规则,默认读取所有context 文件的getConfigLocations()函数。
  默认读取所有context文件的getConfigLocations()函数对速度和测试的隔离化都有影响,可以在子类重新实现。不过自己重新一个个写相关context文件也好烦,而且其实在全lazy-load的情况下,速度也还可以接受。如何取舍要自己平衡了。
  对于CRUD的测试,在helloworld示例里的变量名都作了泛化,可以快速copy到另一个测试里。
  另外,留意resources/spring/test 下的文件,利用了Spring的PropertyOverrideConfigurer,重新设定测试时的ApplicatonContext 里各个Bean的属性如指定测试用的DataSource,详细用法见Spring配置要点。
  phpcode有点排版不便,大家可以看wiki原文。

Filed In 学习路上 | Study | Tags: , , | Add a Comment »

Your Comments.

Leave your own response

Leave a Reply.

Comment Form.

Fields denoted with a "*" are required. Your comment may require moderation, please be patient.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>




Your Reply.

Use the following link to trackback from your own site:
http://www.zhangzhang.net/2006/12/sping-project-testing/trackback/

Spread the word.

Octopus supports RSS feed for this post RSS 2.0 , and Trackbacks from other blogs.


Fatal error: Call to undefined function get_single_sidebar() in /home/zhangzha/public_html/wp-content/themes/octopus/single.php on line 69