How to write unit test for sub controller action

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to write unit test for sub controller action

hbbg
Hi, I am using grails -1.3.7 and
have  the following controller:

class MyController extends OtherController{
    def create = {
        def obj= new SomeDomain()
        //set obj parameters
        obj.save(flush: true)
        render "created"
    }
}

I am trying to write unit test for this action, but get the following exception:

java.lang.NoClassDefFoundError: my.pack.OtherController
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getDeclaredConstructors(Class.java:1836)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at grails.test.GrailsUnitTestCase.registerMetaClass(GrailsUnitTestCase.groovy:104)
    at grails.test.GrailsUnitTestCase.mockController(GrailsUnitTestCase.groovy:174)
    at grails.test.ControllerUnitTestCase.setUp(ControllerUnitTestCase.groovy:54)
    at my.pack.MyControllerTests.super$7$setUp(MyControllerTests.groovy)
    at my.pack.MyControllerTests.setUp(MyControllerTests.groovy:9)

Here is my unit test:

class MyControllerTests extends ControllerUnitTestCase {  
    void testCreate(){
        def controller = new MyController()      
        controller.params.param1 = "val1"        
        mockDomain(SomeDomain)
        controller.create()    
        assertEquals("created" , controller.response.contentAsString)        
   }
}

How can I inject parent controller to pass test?
Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

Nathan Wells
Do you have annotations on your test class? I'm not sure, but you may need to mock both the parent and subclass.

Nathan Wells


On Mon, Oct 29, 2012 at 9:28 AM, hbbg <[hidden email]> wrote:
Hi, I am using grails -1.3.7 and
have  the following controller:

class MyController extends OtherController{
    def create = {
        def obj= new SomeDomain()
        //set obj parameters
        obj.save(flush: true)
        render "created"
    }
}

I am trying to write unit test for this action, but get the following
exception:

java.lang.NoClassDefFoundError: my.pack.OtherController
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getDeclaredConstructors(Class.java:1836)
    at java.security.AccessController.doPrivileged(Native Method)
    at
org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at
grails.test.GrailsUnitTestCase.registerMetaClass(GrailsUnitTestCase.groovy:104)
    at
grails.test.GrailsUnitTestCase.mockController(GrailsUnitTestCase.groovy:174)
    at
grails.test.ControllerUnitTestCase.setUp(ControllerUnitTestCase.groovy:54)
    at my.pack.MyControllerTests.super$7$setUp(MyControllerTests.groovy)
    at my.pack.MyControllerTests.setUp(MyControllerTests.groovy:9)

Here is my unit test:

class MyControllerTests extends ControllerUnitTestCase {
    void testCreate(){
        def controller = new MyController()
        controller.params.param1 = "val1"
        mockDomain(SomeDomain)
        controller.create()
        assertEquals("created" , controller.response.contentAsString)
   }
}

How can I inject parent controller to pass test?




--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-write-unit-test-for-sub-controller-action-tp4637146.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



Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

hbbg
Hi Nathan,
no, I have no  annotations and I can't find anything  for this grails version in documentation.
Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

Nathan Wells
Right... 1.3.7...

I don't have a lot of experience testing in 1.3.7. Testing got a lot easier and better documented with 2.0. You might try adding a mockController(clazz) for both MyController and OtherController. Other than that, I would probably have to resort to diving into the source for ControllerUnitTestCase and it's parent classes.
 
Nathan Wells


On Tue, Oct 30, 2012 at 2:13 AM, hbbg <[hidden email]> wrote:
Hi Nathan,
no, I have no  annotations and I can't find anything  for this grails
version in documentation.



--
View this message in context: http://grails.1312388.n4.nabble.com/How-to-write-unit-test-for-sub-controller-action-tp4637146p4637177.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



Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

Jeff Brown-4
In reply to this post by Nathan Wells

On Oct 29, 2012, at 10:59 PM, Nathan Wells <[hidden email]> wrote:

>
>
> Here is my unit test:
>
> class MyControllerTests extends ControllerUnitTestCase {
>     void testCreate(){
>         def controller = new MyController()
>         controller.params.param1 = "val1"
>         mockDomain(SomeDomain)
>         controller.create()
>         assertEquals("created" , controller.response.contentAsString)
>    }
> }
>
> How can I inject parent controller to pass test?
>


I don't know what you mean by "inject parent controller" but you should be able to test the create action in MyController without any mention of the parent controller at all.  You also should not need to be creating an instance of MyController like you are there.  You should refer to the instance provided to the test by the parent class (ControllerUnitTestCase).

See these...

https://github.com/jeffbrown/testsubcontroller/blob/master/grails-app/controllers/com/demo/OtherController.groovy
https://github.com/jeffbrown/testsubcontroller/blob/master/grails-app/controllers/com/demo/MyController.groovy
https://github.com/jeffbrown/testsubcontroller/blob/master/test/unit/com/demo/MyControllerTests.groovy


Is that helpful?



JSB
--
Jeff Brown
SpringSource
http://www.springsource.com/

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


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

hbbg
Thanks very much Jeff and Nathan.. problem was that in OtherController I have declared config variables (using ConfigurationHolder.config) which I am not configuring in test.

I have tried with grailsApplication and with ConfigurationHolder, but still can't setup config variables for test controller..
Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

hbbg
I have also tried to write

      mockConfig '''
            someURL = 'aa'
        '''
And

        def mockedConfig = new ConfigObject()
        mockedConfig.someURL = "aa"        
        ConfigurationHolder.config = mockedConfig

in setUp method, but  nothing works.

The last version works for test service but not for test controller.

Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

Jeff Brown-4

On Nov 1, 2012, at 10:12 AM, hbbg <[hidden email]> wrote:

> I have also tried to write
>
>      mockConfig '''
>            someURL = 'aa'
>        '''
> And
>
>        def mockedConfig = new ConfigObject()
>        mockedConfig.someURL = "aa"        
>        ConfigurationHolder.config = mockedConfig
>
> in setUp method, but  nothing works.
>
> The last version works for test service but not for test controller.
>



If you created the simplest application possible which demonstrates your problem/question and then put that application somewhere that the community can access it (get a free github account if you don't have one), that would probably increase the chance of getting to the bottom of this.  The code that I linked earlier is in a project that it took  me probably 3 minutes to create and publish.  Having something real to work with may help drill into whatever problem you are having.  If it is simpler for you, fork and modify the application that I linked earlier.



JSB
--
Jeff Brown
SpringSource
http://www.springsource.com/

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


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

hbbg
Hi Jeff,
thanks for advice. I have changed your code in github.

Can you please view changes?
Reply | Threaded
Open this post in threaded view
|

Re: How to write unit test for sub controller action

hbbg
Here changed code:

class MyController extends OtherController{

    def create = {
        render 'MyController.created'
    }
}

class OtherController {

    static String someURL = CH.config.someURL

    def index = { }
}

class MyControllerTests extends ControllerUnitTestCase {

//    def grailsApplication

    protected void setUp() {
        super.setUp()

        /*mockConfig '''
            someURL = 'aa'
        '''*/

        /*def mockedConfig = new ConfigObject()
        mockedConfig.someURL = "aa"
        ConfigurationHolder.config = mockedConfig*/

//        grailsApplication.config.someURL="aa"
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCreate() {
        controller.create()
        assert 'MyController.created' == controller.response.contentAsString
    }
}

My problem thah I cant't declare someURL config variable in MyController.