Spock with Grails

In this post, i hope to cover installing, configuring and using Spock testing framework with Grails. Spock is a testing and specification framework for Java and Groovy applications.

Environment at the writing: jdk1.7.0_07, Grails 2.1.1

1. Installing & Configuring Spock Plugin

One Way – Configure Spock by adding following line in \grails-app\conf\BuildConfig.groovy:

At compile time, Grails will detect that you have configured Spock and install the plugin

Second Way – Run command ‘grails install-plugin spock’ from command line and it will install&configure the Spock for you

To verify at any time, open the dependency report by calling command ‘grails dependency-report” or ‘open dep-report’ from interactive grails console. The report will look like the following:

From the report, look at the  ‘test’ tab to see the ‘Spock’ Plugin

2. Implementing

For Unit Tests – extend your Test class with ‘UnitSpec’ and import the library as following:

For Integration Tests – extend your Test class with ‘IntegrationSpec’ and import the library as following:

4. Running Tests

To Run Unit Tests – one way is to call command ‘grails test-app unit:spock <nameOfTestClass>.<nameOfTest>’ . The other way is depending on your IDE  (IntellJ right click from test itself->Run)

To Run Integration Tests – one way is to call command ‘grails test-app integration:spock <nameOfTestClass>.<nameOfTest>’. The other way is depending on your IDE (IntellJ right click from test itself ->Run)

5. Other

Sharing or Reusing – Spock allows an easy way to reuse or share an object among the individual tests(method features) as following:

Its accomplished by adding notation ‘@Shared’ when declaring the object. Also, don’t forget to import ‘spock.lang.Shared’ package

Sources:

 

Issues Encountered:

1. “Cannot get property ‘autowireCapableBeanFactory’ on null object”. We were getting these because for Integration test we cannot use Unit test mixines, so by dropping @TestFor(class) and @TestMixin(GrailsUnitTestMixin) it solved the problem

Grails Custom Validator Snippets

This post is going to be updated on-going and is a space where i hope to collect different examples of custom validators that ensure  some constraint for a Domain class in Grails Framework

1. Ensure specific Date format constraint (i.e. ‘Jan 22, 2011’)

String submitDate 
...
final static DateFormat DATEFORMAT  = DateFormat.getDateInstance(DateFormat.MEDIUM)
static constraints = {
      submitDate validator: {
         try{
             Date date = DATEFORMAT.parse(it)
             return DATEFORMAT.format(date) == it
         }catch(ParseException exception){
             return false
         }
      }

2. Zip Code validator

static constraint = {
   zipCode blank: false, size: 5..10, validator:{zip ->
            (zip ==~ /^(\d{5}-\d{4})|(\d{5})$/) ? true : false
        }
}

3. Numeric validator for Phone number,Social Security number,etc
a) If you know exactly how many numbers should contain. Here is example for 10:

static constraints = {
     ...
        phone matches: '\\d{10}'
     ...
    }

b) If its necessary to validate for some specific pattern such as 651-555-5555

static constraints = {
     ...
        phone matches: '\\d{3}\\-\\d{3}\\-\\d{4}'
     ...
    }

c)If its necessary to validate that it contains only numbers

static constraints = {
     ...
        phone matches: '\\d+'
     ...
    }