Plastic Criteria Plugin

  • Tags: /
  • Latest: 1.5
  • Last Updated: 23 February 2014
  • Grails version: 1.3 > *
  • Authors: Fabio Issamu Oshiro
  • Organization: Investtools
1 vote
Dependency:
test ":plastic-criteria:1.5"

 Documentation  Source  Issues

Summary

Mock Grails Criteria for Unit Tests

Installation

Grails 1.3.7

grails install-plugin plastic-criteria
or
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

If you dont know why the results are wrong try:

grails -echoOut test-app unit:

Grails 2.x

package plastic.test

import 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.plastic

import grails.test.* import static plastic.criteria.PlasticCriteria.*; // mockCriteria() static method

class 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 } }