Quantcast

Call a method on the target class when using own @TestMixin

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Call a method on the target class when using own @TestMixin

Marcin Gryszko-2
I want to migrate a Grails 1.3 application to Grails 2.x. In our tests we use a lot of mixins. The code below works correctly in Grails 1.3 and Grails 2.0 without any specific Grails 2.0 test mixin:

// FIRST EXAMPLE - test passes

@Category(Specification)
class AGroovyMixin {

    def methodInMixinThatInvokesMethodOfSpec() {
        println 'the method in the mixin is called'
        methodOfSpecInvokedInMixin()
    }
}

@Mixin(AGroovyMixin)
class FirstSpec extends Specification {

    def invocations = 0

    def methodOfSpecInvokedInMixin() {
        invocations++
    }

    def 'check that that a method of this spec has been called in the mixin'() {
        when:
        methodInMixinThatInvokesMethodOfSpec()

        then:
        invocations > 0
    }
}


In Grails 2.0, if I want to mix Grails test mixins (@Mock, @TestMixin) with our own categories, my category is not mixed into the test class. It means, you cannot use @Mock or @TestMixin with the vanilla @Mixin.

Alternatively, if I try to mix in my category using the @TestMixin, the category has no access to the test class code (see the example below).

// SECOND EXAMPLE - est fails

class AGrailsTestMixin {

    def methodInMixinThatInvokesMethodOfSpec() {
        println 'the method in the mixin is called'
        methodOfSpecInvokedInMixin()
    }
}

@TestMixin(AGrailsTestMixin)
class SecondSpec extends Specification {

    def invocations = 0

    def methodOfSpecInvokedInMixin() {
        invocations++
    }

    def 'check that that a method of this spec has been called in the test mixin'() {
        when:
        methodInMixinThatInvokesMethodOfSpec()

        then:
        invocations > 0
    }
}

I would like to combine @Mixin with @Mock, or have an implementation of @TestMixin that can access methods on the target class. What could I do?

Cheers,

Marcin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

pledbrook
> I would like to combine @Mixin with @Mock, or have an implementation of
> @TestMixin that can access methods on the target class. What could I do?

This is really a question for the user mailing list although it
probably makes sense to discuss potential solutions here because it
may require changes to Grails. Or maybe Groovy.

Does anyone know why @Mixin doesn't play nice with the new test annotations?

--
Peter Ledbrook
Developer Advocate
VMware

t: @pledbrook

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

tudor.malene
There is a Jira ticket for this:

GRAILS-8652



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

Graeme Rocher
Administrator
In reply to this post by pledbrook
The metaclass is cleaned up after each run which removes the mixin methods, you have to reapply the mixin with MyClass.metaClass.mixin MyMixin in the setup of the method

Cheers


On Wed, Nov 14, 2012 at 9:15 AM, Peter Ledbrook <[hidden email]> wrote:
> I would like to combine @Mixin with @Mock, or have an implementation of
> @TestMixin that can access methods on the target class. What could I do?

This is really a question for the user mailing list although it
probably makes sense to discuss potential solutions here because it
may require changes to Grails. Or maybe Groovy.

Does anyone know why @Mixin doesn't play nice with the new test annotations?

--
Peter Ledbrook
Developer Advocate
VMware

t: @pledbrook

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

Marcin Gryszko-2
I'll try the solution proposed by Graeme.

Marcin Gryszko      


On Wed, Nov 14, 2012 at 4:52 PM, Graeme Rocher <[hidden email]> wrote:
The metaclass is cleaned up after each run which removes the mixin methods, you have to reapply the mixin with MyClass.metaClass.mixin MyMixin in the setup of the method

Cheers


On Wed, Nov 14, 2012 at 9:15 AM, Peter Ledbrook <[hidden email]> wrote:
> I would like to combine @Mixin with @Mock, or have an implementation of
> @TestMixin that can access methods on the target class. What could I do?

This is really a question for the user mailing list although it
probably makes sense to discuss potential solutions here because it
may require changes to Grails. Or maybe Groovy.

Does anyone know why @Mixin doesn't play nice with the new test annotations?

--
Peter Ledbrook
Developer Advocate
VMware

t: @pledbrook

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

Antony Jones
Can this be changed in Grails? It is absolutely mind-boggling (even though the answer is simple).


On 14 November 2012 21:11, Marcin Gryszko <[hidden email]> wrote:
I'll try the solution proposed by Graeme.

Marcin Gryszko      



On Wed, Nov 14, 2012 at 4:52 PM, Graeme Rocher <[hidden email]> wrote:
The metaclass is cleaned up after each run which removes the mixin methods, you have to reapply the mixin with MyClass.metaClass.mixin MyMixin in the setup of the method

Cheers


On Wed, Nov 14, 2012 at 9:15 AM, Peter Ledbrook <[hidden email]> wrote:
> I would like to combine @Mixin with @Mock, or have an implementation of
> @TestMixin that can access methods on the target class. What could I do?

This is really a question for the user mailing list although it
probably makes sense to discuss potential solutions here because it
may require changes to Grails. Or maybe Groovy.

Does anyone know why @Mixin doesn't play nice with the new test annotations?

--
Peter Ledbrook
Developer Advocate
VMware

t: @pledbrook

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com




--
________________________________
ꜽ . antony jones . http://www.enzy.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Call a method on the target class when using own @TestMixin

Marcin Gryszko-2
In reply to this post by Graeme Rocher
Tested the Graeme's hint and it works.

I agree with Antony that Grails mixins should be compatible with Groovy mixins. Should I file a JIRA issue for this?

Marcin Gryszko      


On Wed, Nov 14, 2012 at 4:52 PM, Graeme Rocher <[hidden email]> wrote:
The metaclass is cleaned up after each run which removes the mixin methods, you have to reapply the mixin with MyClass.metaClass.mixin MyMixin in the setup of the method

Cheers


On Wed, Nov 14, 2012 at 9:15 AM, Peter Ledbrook <[hidden email]> wrote:
> I would like to combine @Mixin with @Mock, or have an implementation of
> @TestMixin that can access methods on the target class. What could I do?

This is really a question for the user mailing list although it
probably makes sense to discuss potential solutions here because it
may require changes to Grails. Or maybe Groovy.

Does anyone know why @Mixin doesn't play nice with the new test annotations?

--
Peter Ledbrook
Developer Advocate
VMware

t: @pledbrook

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

Loading...