Skip to content

Commit 0745357

Browse files
committed
fix for GRAILS-5669 "On a controller integration test, calling render with a GSP on a base controller results in Cannot set readonly property: modelAndView"
1 parent 3d681f4 commit 0745357

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/java/grails/test/MockUtils.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import org.springframework.beans.SimpleTypeConverter
3737
import org.springframework.mock.web.MockHttpSession
3838
import org.springframework.validation.Errors
3939
import org.springframework.web.context.request.RequestContextHolder
40+
import org.springframework.web.servlet.ModelAndView;
41+
4042
import grails.validation.ValidationException
4143

4244
/**
@@ -103,7 +105,7 @@ class MockUtils {
103105
def renArgs = [:]
104106
def chaArgs = [:]
105107
def template = [:]
106-
def modelAndView = [:]
108+
def modelAndView = new ModelAndView()
107109
clazz.metaClass.getForwardArgs = {-> fwdArgs}
108110
clazz.metaClass.getRedirectArgs ={-> redArgs}
109111
clazz.metaClass.getRenderArgs ={-> renArgs}
@@ -227,6 +229,7 @@ class MockUtils {
227229

228230
clazz.metaClass.getTemplate = {-> template}
229231
clazz.metaClass.getModelAndView = {-> modelAndView}
232+
clazz.metaClass.setModelAndView = { ModelAndView mv -> modelAndView = mv}
230233
}
231234

232235
/**

src/test/grails/test/ControllerUnitTestCaseTests.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package grails.test;
22

3+
import org.springframework.web.servlet.ModelAndView;
4+
35
/**
46
* Test case for {@link ControllerUnitTestCase}.
57
*/
@@ -25,9 +27,22 @@ class ControllerUnitTestCaseTests extends GroovyTestCase {
2527
testCase.testCommandObject()
2628
testCase.tearDown()
2729
}
30+
31+
void testGetSetModelAndView() {
32+
def testCase = new UnitTestControllerTestCase()
33+
testCase.setUp()
34+
testCase.testModelAndView()
35+
testCase.tearDown()
36+
37+
}
2838
}
2939

3040
class UnitTestControllerTestCase extends ControllerUnitTestCase {
41+
42+
void testModelAndView() {
43+
controller.testSetModelAndView()
44+
}
45+
3146
void testControllerClass() {
3247
assertEquals UnitTestController, controllerClass
3348
assertEquals "unitTest", controller.controllerName
@@ -74,6 +89,11 @@ class UnitTestController {
7489
def index = {
7590

7691
}
92+
93+
def testSetModelAndView() {
94+
modelAndView = new ModelAndView()
95+
println modelAndView.viewName
96+
}
7797
}
7898

7999
class ControllerUnitTestCaseCommandObject {

0 commit comments

Comments
 (0)