MissingMethodException on response.contentAsString after upgrade

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

MissingMethodException on response.contentAsString after upgrade

Jacek Furmankiewicz
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/3714187d-6238-4bc2-97bb-f85e5b2c967e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jeff Brown

> On Apr 19, 2016, at 10:04 AM, Jacek Furmankiewicz <[hidden email]> wrote:
>
> 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
> 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/

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CC0797BD-6971-4529-8702-46554B2AF471%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jeff Brown

> On Apr 19, 2016, at 11:27 AM, Jeff Scott Brown <[hidden email]> wrote:
>
>>
>> On Apr 19, 2016, at 10:04 AM, Jacek Furmankiewicz <[hidden email]> wrote:
>>
>> 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
>> 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'
>    }
> }
>
>
>

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
    }
}


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/

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/C3FFCE47-FDD8-4A1B-BFD4-97FB680E1A1D%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jacek Furmankiewicz
In reply to this post by Jacek Furmankiewicz
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....

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/4d752b9c-aa7d-4b99-a9d4-06d0991d175c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jeff Brown

> On Apr 19, 2016, at 12:58 PM, Jacek Furmankiewicz <[hidden email]> 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
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/

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/A07B240C-7285-4E48-9551-44F245B81CF8%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jeff Brown

> On Apr 19, 2016, at 2:07 PM, Jeff Scott Brown <[hidden email]> wrote:
>
>>
>> On Apr 19, 2016, at 12:58 PM, Jacek Furmankiewicz <[hidden email]> 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.



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/

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/F95DC71D-3A5E-4AB9-B48E-83E4C0F729BC%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jacek Furmankiewicz
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....

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Colin Harrington-3
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.


On Tue, Apr 19, 2016 at 2:25 PM, Jacek Furmankiewicz <[hidden email]> wrote:
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....

--
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 [hidden email].
To post to this group, send email to [hidden email].
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAMRV%3Dg5VwYEi7Di5d1%3DhGzM1SDoCTQ_iOD4dp4t9VVtEbpERFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MissingMethodException on response.contentAsString after upgrade

Jacek Furmankiewicz
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/f3f1fce3-d4fd-4179-9642-cb1a880c2308%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...