MissingMethodException on response.contentAsString after upgrade

27 views
Skip to first unread message

Jacek Furmankiewicz

unread,
Apr 19, 2016, 11:04:50 AM4/19/16
to Grails Dev Discuss
Hi,

I am attempting to port an old Grails 2 app to the latest and greatest of everything: Gradle, Groovy, Grails, etc.

I am getting a whole set of MissingMethodException on unit tests, when calling controller.response.contentAsString, e.g.

@TestFor(SysParamController)
class SysParamControllerTests {

   
@Test
    public void testNoParams() {
       
controller.list()
       
assertEquals("Invalid request", controller.response.contentAsString)
   
}

It seems the contentAsString method on the response is not available any more (it is even highlighted as such as IntelliJ when I try to open it).

This code is in a unit test that is now in the src/test/groovy folder after the upgrade to Grails 3.1.4.

Any suggestions as to what I am missing would be welcome. Been stuck on this for 2 days now and not getting ahead anywhere...

Thank you
Jacek

Jeff Scott Brown

unread,
Apr 19, 2016, 12:27:42 PM4/19/16
to grails-de...@googlegroups.com
> Jack
>

You could refer to response.text. If I were to write that test using Spock, it would look something like this:

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(SysParamController)
class SysParamControllerSpec extends Specification {

void "test no params"() {
when:
controller.list()

then:
response.text == 'Invalid request'
}
}





JSB


Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

Jeff Scott Brown

unread,
Apr 19, 2016, 12:37:43 PM4/19/16
to grails-de...@googlegroups.com
You have a number of options. Here are several Junit examples:

import grails.test.mixin.TestFor
import org.junit.Test

import static org.junit.Assert.assertEquals

@TestFor(SysParamController)
class SysParamControllerSpec {

@Test
public void testNoParams() {
controller.list()
assert "Invalid request" == controller.response.contentAsString
}

@Test
public void testNoParams2() {
controller.list()
assert "Invalid request" == response.contentAsString
}

@Test
public void testNoParams3() {
controller.list()
assert "Invalid request" == response.text
}

@Test
public void testNoParams4() {
controller.list()
assertEquals "Invalid request", controller.response.contentAsString
}


@Test
public void testNoParams5() {
controller.list()
assertEquals "Invalid request", response.contentAsString
}

@Test
public void testNoParams6() {
controller.list()
assertEquals "Invalid request", response.text

Jacek Furmankiewicz

unread,
Apr 19, 2016, 1:58:57 PM4/19/16
to Grails Dev Discuss
Sorry, but it is still the exact same error:

gsp.SysParamControllerTests > testNoParams FAILED
    groovy.lang.MissingMethodException at SysParamControllerTests.groovy:

@TestFor(SysParamController)
class SysParamControllerTests {

   
@Test
    public void testNoParams() {
       
controller.list()

       
assertEquals("Invalid request", controller.response.text)
   
}

So changing crom contentAsString to text made no difference.

Was this removed in a newer version of Groovy or Grails? We've had this unit test for years, it only broke once we upgraded....

Jeff Scott Brown

unread,
Apr 19, 2016, 3:07:21 PM4/19/16
to grails-de...@googlegroups.com

> On Apr 19, 2016, at 12:58 PM, Jacek Furmankiewicz <jac...@gmail.com> wrote:
>
> Sorry, but it is still the exact same error:
>
> gsp.SysParamControllerTests > testNoParams FAILED
> groovy.lang.MissingMethodException at SysParamControllerTests.groovy:
>
> @TestFor(SysParamController)
> class SysParamControllerTests {
>
> @Test
> public void testNoParams() {
> controller.list()
> assertEquals("Invalid request", controller.response.text)
> }
>
> So changing crom contentAsString to text made no difference.


What is the MissingMethodException message? If it is complaining about assertEquals, you are probably missing something like the following:

import static org.junit.Assert.assertEquals

The test I showed above runs, and works.



JSB

Jeff Scott Brown

unread,
Apr 19, 2016, 3:10:51 PM4/19/16
to grails-de...@googlegroups.com

> On Apr 19, 2016, at 2:07 PM, Jeff Scott Brown <bro...@ociweb.com> wrote:
>
>>
>> On Apr 19, 2016, at 12:58 PM, Jacek Furmankiewicz <jac...@gmail.com> wrote:
>>
>> Sorry, but it is still the exact same error:
>>
>> gsp.SysParamControllerTests > testNoParams FAILED
>> groovy.lang.MissingMethodException at SysParamControllerTests.groovy:
>>
>> @TestFor(SysParamController)
>> class SysParamControllerTests {
>>
>> @Test
>> public void testNoParams() {
>> controller.list()
>> assertEquals("Invalid request", controller.response.text)
>> }
>>
>> So changing crom contentAsString to text made no difference.
>
>
> What is the MissingMethodException message? If it is complaining about assertEquals, you are probably missing something like the following:
>
> import static org.junit.Assert.assertEquals
>
> The test I showed above runs, and works.
>

See a runnable example at https://github.com/jeffbrown/furmankiewicz/blob/master/src/test/groovy/demo/SysParamControllerTest.groovy.

Jacek Furmankiewicz

unread,
Apr 19, 2016, 3:25:39 PM4/19/16
to Grails Dev Discuss
Hi Jeff, you were right, many of these tests were missing the static import for Assert, not sure how they worked before.

Quick additional question, a lot of the tests are also failing on lines like this:

locationService = mockFor(LocationService)

namely the mockFor method. Has that been replaced with a different apporach in the newer version of Grails?
I also get a MissingMethodException on those....

Colin Harrington

unread,
Apr 19, 2016, 6:36:28 PM4/19/16
to grails-de...@googlegroups.com
Hi Jacek, 

mockFor(...) and the GrailsMock that was utilized in previous versions of Grails have been removed in 3.x. 
  
Rather than Grails providing yet another means of managing mocks & stubs, it is recommended that you utilize Spock and mock collaborators with Spock's interaction based testing.  

With Spock, this is what it would look like:
void "test something"() {
given:
controller.locationService = Mock(LocationService)

when: "executing an action that uses the locationService"
controller.locateSomething()

then: "verify locate() was called with 'something' as a parameter"
1 * controller.locationService.locate('something')
}
It is important to remember that with unit testing, you are just testing one unit such as a method or class and mocking out all of the collaborators around it.


--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/43e88bec-fab5-4364-9b67-c505bcd31d94%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Colin Harrington
Grails Developer 
@ColinHarrington

Jacek Furmankiewicz

unread,
Apr 20, 2016, 10:02:05 AM4/20/16
to Grails Dev Discuss
Darn, that's unfortunate. I was hoping I can just port this old app and not have to rewrite whole portions of it.

Either way thanks to all of you for pointing me in the right direction.
Reply all
Reply to author
Forward
0 new messages