Quantcast

How to unit test a service method that uses a 'where' query?

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

How to unit test a service method that uses a 'where' query?

marcopas
Hi there,

 i am having difficulties writing a test for a service method that
contains a 'where' query. Whatever i seem to do i am getting errors.
When running the code normally inside my application the 'where' query
works as expected. So i guess that i must be missing something.

Is it possible to unit test a service method that contains a 'where'
query using Spock/Build-Test-Data plugin?

/Marco

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

Re: How to unit test a service method that uses a 'where' query?

asinesio
Marco,

Assuming you're using Grails 2.0.x -- did you add a @Mock([MyDomainClass]) annotation to the top of your test class?  This adds the mock GORM methods to your domain class(es) in question and should cover your findAllWhere() method.

Some more advanced GORM queries do not work in unit tests (e.g. criteria, executeQuery, and other HQL).  

You can try either writing your own static method on the metaClass to mock out the behavior  (my approach) or some people like using the Plastic Criteria Grails plugin: http://www.grails.org/plugin/plastic-criteria

To add your own method:

MyDomainClass.metaClass.static.executeQuery = { hql -> return [new MyDomainClass(), new MyDomainClass()] } 

Hope this helps,
Andy

On Fri, May 25, 2012 at 1:27 AM, Marco Pas <[hidden email]> wrote:
Hi there,

 i am having difficulties writing a test for a service method that
contains a 'where' query. Whatever i seem to do i am getting errors.
When running the code normally inside my application the 'where' query
works as expected. So i guess that i must be missing something.

Is it possible to unit test a service method that contains a 'where'
query using Spock/Build-Test-Data plugin?

/Marco

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

   http://xircles.codehaus.org/manage_email



Loading...