Quantcast

unit test urlmappings controller not found error

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

unit test urlmappings controller not found error

fiallega
Grails version: 2.0.0.RC3
I am trying to unit test urlmappings as explained in the documentation url mapping
Specifically my code test code looks like this

import myproject.AnimalController
@TestFor(UrlMappings)
class UrlMappingsTests{

    @Test
    void testSomething() {
                assertForwardUrlMapping("/action1", controller: 'animal',action: "show")    
        }
}

and the urlmappings class is

class UrlMappings {

        static mappings = {
                "/$controller/$action?/$id?"{
                        constraints {
                                // apply constraints here
                        }
                }

                "/action1"(controller: "animal", action: "show")
               
                "/"(view:"/index")
                "500"(view:'/error')
        }
}

I get the following error:
junit.framework.AssertionFailedError: Url mapping assertion for '/action1' failed, 'animal' is not a valid controller
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
        at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:81)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
        at grails.test.mixin.web.UrlMappingsUnitTestMixin.assertController(UrlMappingsUnitTestMixin.groovy:104)



It calls my attention the comment made in the documentation:
assertForwardUrlMapping - Asserts a URL mapping is forwarded for the given controller class (note that controller will need to be defined as a mock collaborate for this to work)

Not sure how to make it work. I also looked at a couple of JIRA tickets but none really I think applied.
When I look at the code for the UrlMappingsUnitTestMixin.groovy class what is failing is the call to
grailsApplication.getArtefactByLogicalPropertyName(ControllerArtefactHandler.TYPE, controller).
That is really as far as can take it. The whole unittesting in grails seems a bit misterious to me, in what has been loaded and what we have access to.

Any help would be appreciated
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: unit test urlmappings controller not found error

David Trattnig
Add a mock annotation for your AnimalController and it should work fine

@TestFor(UrlMappings)
@Mock(AnimalController)
class UrlMappingsTests{
...

cheers
david

On Tue, Dec 13, 2011 at 3:48 PM, fiallega <[hidden email]> wrote:
Grails version: 2.0.0.RC3
I am trying to unit test urlmappings as explained in the documentation
http://grails.org/doc/2.0.x/guide/testing.html#unitTestingURLMappings url
mapping
Specifically my code test code looks like this

import myproject.AnimalController
@TestFor(UrlMappings)
class UrlMappingsTests{

   @Test
   void testSomething() {
               assertForwardUrlMapping("/action1", controller: 'animal',action: "show")
       }
}

and the urlmappings class is

class UrlMappings {

       static mappings = {
               "/$controller/$action?/$id?"{
                       constraints {
                               // apply constraints here
                       }
               }

               "/action1"(controller: "animal", action: "show")

               "/"(view:"/index")
               "500"(view:'/error')
       }
}

I get the following error:
junit.framework.AssertionFailedError: Url mapping assertion for '/action1'
failed, 'animal' is not a valid controller
       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
       at
org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
       at
org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)
       at
org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:81)
       at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
       at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
       at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
       at
grails.test.mixin.web.UrlMappingsUnitTestMixin.assertController(UrlMappingsUnitTestMixin.groovy:104)



It calls my attention the comment made in the documentation:
assertForwardUrlMapping - Asserts a URL mapping is forwarded for the given
controller class (*note that controller will need to be defined as a mock
collaborate for this to work*)

Not sure how to make it work. I also looked at a couple of JIRA tickets but
none really I think applied.
When I look at the code for the UrlMappingsUnitTestMixin.groovy class what
is failing is the call to
grailsApplication.getArtefactByLogicalPropertyName(ControllerArtefactHandler.TYPE,
controller).
That is really as far as can take it. The whole unittesting in grails seems
a bit misterious to me, in what has been loaded and what we have access to.

Any help would be appreciated

--
View this message in context: http://grails.1312388.n4.nabble.com/unit-test-urlmappings-controller-not-found-error-tp4190690p4190690.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email



Loading...