/
ControllerUnitTestCaseTests.groovy
107 lines (85 loc) · 2.53 KB
/
ControllerUnitTestCaseTests.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package grails.test;
import org.springframework.web.servlet.ModelAndView;
/**
* Test case for {@link ControllerUnitTestCase}.
*/
class ControllerUnitTestCaseTests extends GroovyTestCase {
void testControllerClass() {
UnitTestControllerTestCase testCase = new UnitTestControllerTestCase()
testCase.setUp()
testCase.testControllerClass()
testCase.tearDown()
}
void testExplicitControllerClass() {
new OtherTestCase().testControllerClass()
}
/**
* Tests that the {@link ControllerUnitTestCase#mockCommandObject(Class)}
* method works.
*/
void testMockCommandObject() {
def testCase = new UnitTestControllerTestCase()
testCase.setUp()
testCase.testCommandObject()
testCase.tearDown()
}
void testGetSetModelAndView() {
def testCase = new UnitTestControllerTestCase()
testCase.setUp()
testCase.testModelAndView()
testCase.tearDown()
}
}
class UnitTestControllerTestCase extends ControllerUnitTestCase {
void testModelAndView() {
controller.testSetModelAndView()
}
void testControllerClass() {
assertEquals UnitTestController, controllerClass
assertEquals "unitTest", controller.controllerName
assertNull controller.actionName
webRequest.actionName = "foo"
assertEquals "foo", controller.actionName
}
void testCommandObject() {
mockCommandObject(ControllerUnitTestCaseCommandObject)
def cmd = new ControllerUnitTestCaseCommandObject()
assertFalse cmd.validate()
assertEquals 2, cmd.errors.errorCount
assertEquals "nullable", cmd.errors["name"]
assertEquals "range", cmd.errors["age"]
cmd.name = "Pedro"
cmd.age = 5
assertFalse cmd.validate()
assertEquals 1, cmd.errors.errorCount
assertEquals "range", cmd.errors["age"]
cmd.age = 24
assertTrue cmd.validate()
assertFalse cmd.errors.hasErrors()
}
}
class OtherTestCase extends ControllerUnitTestCase {
OtherTestCase() {
super(UnitTestController)
}
void testControllerClass() {
assertEquals UnitTestController, controllerClass
}
}
class UnitTestController {
String name
def index = {
}
def testSetModelAndView() {
modelAndView = new ModelAndView()
println modelAndView.viewName
}
}
class ControllerUnitTestCaseCommandObject {
String name
int age
static constraints = {
name(nullable: false, blank: false)
age(range: 10..50)
}
}