Plastic Criteria Plugin
Dependency :
test ":plastic-criteria:1.0"
Summary
Mock Grails Criteria for Unit Tests
Installation
Grails 1.3.7
grails install-plugin plastic-criteria
grails install-plugin http://grails.org/plugins/grails-plastic-criteria/tags/RELEASE_0_9/grails-plastic-criteria-0.9.zip
Grails 2.x
Edit BuildConfig.groovy and add test ":plastic-criteria:0.9"plugins{
test ":plastic-criteria:0.9"
}Description
How 2 mock criteria in unit tests
Grails 2.x
package plastic.testimport grails.test.mixin.*import static plastic.criteria.PlasticCriteria.* ; // mockCriteria() method@TestFor(Product) class ProductTests { void testSomething() { new Product(name: 'Foo', value: 10).save() new Product(name: 'Foo', value: 20).save() new Product(name: 'Bar', value: 200).save() new Product(name: 'Bar', value: 100).save() // replace default criteria mock mockCriteria([Product]) def results = Product.withCriteria{ projections{ groupProperty('name') // now you have groupProperty sum('value') } } assert [['Foo', 30 ], ['Bar', 300]] == results } }
Grails 1.3.x
package test.plasticimport grails.test.* import static plastic.criteria.PlasticCriteria.*; // mockCriteria() static methodclass BookTests extends GrailsUnitTestCase { protected void setUp() { super.setUp() mockDomain(Product) mockCriteria([Product]) } protected void tearDown() { super.tearDown() } void testSomething() { new Product(name: 'Foo', value: 10).save() new Product(name: 'Foo', value: 20).save() new Product(name: 'Bar', value: 200).save() new Product(name: 'Bar', value: 100).save() mockCriteria([Product]) // criteria mock def results = Product.withCriteria{ projections{ groupProperty('name') // you have groupProperty! sum('value') } } assert [['Foo', 30 ], ['Bar', 300]] == results } }