Grails withCriteria testing

I would like to check the closure of "withCriteria" and don't know how to do it. I see how to mock a call withCriteria, but not check the code in closing. When I run a test that runs "withCriteria", I continue to get a MissingMethodException, although the code works fine with a normal thread of execution. Any ideas? Thank you Steve

+7
grails groovy
source share
4 answers

I would not go along this route. Instead, I would move the request to the domain class as a static search method and test it directly in the integration test with real data. Then you can easily mock the helper method when it is called in a controller or service test.

class YourDomainClass { ... static List<YourDomainClass> findFooBar() { YourDomainClass.withCriteria { ... } } } 

Then in unit test:

 def results = [instance1, instance2, instance3] YourDomainClass.metaClass.static.findFooBar = { -> results } 

Thus, you verify that the query works with the database in memory in the integration test, but it is easy to mock it in unit tests.

+14
source share

In addition to Burt's answer, check for named queries as described here:

http://blog.springsource.com/2010/05/24/more-grails-1-3-features/

You can then make fun of accessing properties / methods in your unit tests, as described in Burt.

+1
source share

Since no one mentioned the possibility of creating a DSL to run other DSLs, the full disclosure of this method is disclosed here. I use it quite often with very good results.

Groovy / Grails tests DSL

+1
source share

Currently there is no sample for the sleep criteria. You will need integration tests. However, Bert’s recommendation to do this static search method is good for organizing code. You should also look at the named queries described at http://www.grails.org/1.2+Release+Notes for good syntax for this.

0
source share

All Articles