Grails中的Controller单元测试
来源:互联网 发布:手机qq2008通用版java 编辑:程序博客网 时间:2024/05/18 09:11
对于Controller的单元测试,Grails也提供了很方便的支持,使得书写非常简单,有代码为证:
单元测试代码
1. class CategoryControllerTests extends GroovyTestCase {
2.
3. void setUp(){
4. def test1= new Category(name: "Test1", parent: null).save()
5. def test2= new Category(name: "Test2", parent: null).save()
6. def test3= new Category(name: "Test3", parent: null).save()
7.
8. def test11= new Category(name: "Test11", parent: test1).save()
9. def test12= new Category(name: "Test12", parent: test1).save()
10. def test21= new Category(name: "Test21", parent: test2).save()
11. }
12.
13. void testListRoot() {
14. def controller= new CategoryController()
15. //categoryList对应返回的model
16. def categoryList= controller.list()?.categoryList
17.
18. assertEquals 3, categoryList.size()
19. assertEquals 'Test1', categoryList[0].name
20. assertEquals 'Test2', categoryList[1].name
21. assertEquals 'Test3', categoryList[2].name
22. }
23.
24. void testListTest1(){
25. def controller= new CategoryController()
26. //其中的params表示的是requestparameter,后面的id是传入的参数。
27. //对于session之类以此类推。
28. controller.params.id= 1
29. def categoryList= controller.list()?.categoryList
30.
31. assertEquals 2, categoryList.size()
32. assertEquals 'Test11', categoryList[0].name
33. assertEquals 'Test12', categoryList[1].name
34. }
35. }
Controller部分代码
1. class CategoryController {
2. def index = { redirect(action:list,params:params) }
3.
4. def allowedMethods = [save:'POST']
5.
6. def list = {
7. if(!params.id){
8. return [ categoryList: Category.findAllByParentIsNull() ]
9. }else{
10. def category= Category.get(params.id)
11. if(category){
12. return [ categoryList: Category.findAll("from Category c where c.parent.id=$params.id"), path: category.getPath()]
13. }else{
14. flash.message = "Category not found with id ${params.id}"
15. redirect(action:list)
16. }
17. }
18. }
19. ......
20. }
以上的代码基本上向开发者隐藏了背后的Mock机制,使用起来也更加简单方便。对于如此简单就能完成Controller的测试,我们没有理由不把TDD进行到底。
- Grails中的Controller单元测试
- 在springMVC中的controller写单元测试
- grails obtain controller action
- Grails Controller层
- Grails Controller层
- controller单元测试
- Spring MVC Controller 单元测试
- springmvc controller 单元测试
- Spring-Controller单元测试
- 对Controller的单元测试
- Spring-Controller单元测试
- Spring Mvc controller单元测试
- mock controller 单元测试
- grails controller 处理 list 实体参数
- 使用grails框架在service中接受controller中的请求值是取出出现[Ljava.lang.String;@d22647形式
- grails 中的验证
- Grails中的Where 查询
- grails中的用户登录
- JVM内存管理:深入垃圾收集器与内存分配策略
- 数据结构与算法分析100例之002(我自己敲的,我是菜鸟~!)
- bash操作符
- php导出excel
- 07年 ZZUPC校赛第二题 津津的储蓄计划(水 ~)
- Grails中的Controller单元测试
- How to add local administrator via cmd
- 如何让vs 2005显示行号
- 转移焦点到另外一个CButton上
- 关于SQL Server 2008实战的读后感
- 精通Struts 2: Web 2.0开发实战的评论
- 关于C#本质论(第2版)的读后感
- Ajax实战——实例详解这本书怎么样
- 强烈推荐UML风格(第2版·英汉对照)