Skip to content

Instantly share code, notes, and snippets.

@staticnull
Created November 26, 2014 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staticnull/67cc573aaa2d9c19f5cc to your computer and use it in GitHub Desktop.
Save staticnull/67cc573aaa2d9c19f5cc to your computer and use it in GitHub Desktop.
Grails 2.2.4 - Custom test phase
import org.codehaus.groovy.grails.test.junit4.JUnit4GrailsTestType
import org.codehaus.groovy.grails.test.support.GrailsTestMode
// http://ldaley.com/post/615966534/custom-grails-test
def cassandraTestTypeName = "cassandra"
def cassandraOrmTestTypeName = "cassandraOrm"
def testMode = new GrailsTestMode(autowire: true, wrapInTransaction: true, wrapInRequestEnvironment: true)
// 1. Add the name of your phase to this variable with this event handler
eventAllTestsStart = {
phasesToRun << cassandraOrmTestTypeName
// The testOptions map contains all switch or parameter style arguments passed to grails test-app)
if (testOptions["with-cassandra"]) {
phasesToRun << cassandraTestTypeName
} else if (testOptions["only-cassandra"]) {
phasesToRun = ["integration"] // Remove the other phases
integrationTests = [] // Remove the normal integration tests
phasesToRun << cassandraTestTypeName
}
}
// 2. Create a custom test type
def cassandraTestType = new JUnit4GrailsTestType(cassandraTestTypeName, cassandraTestTypeName, testMode)
def cassandraOrmTestType = new JUnit4GrailsTestType(cassandraOrmTestTypeName, cassandraOrmTestTypeName, testMode)
// 3. Create a «phase name»Tests variable containing the test type(s)
cassandraTests = [cassandraTestType]
cassandraOrmTests = [cassandraOrmTestType]
// 4. Create pre and post closures
cassandraTestPhasePreparation = {
integrationTestPhasePreparation()
}
cassandraTestPhaseCleanUp = {
integrationTestPhaseCleanUp()
}
cassandraOrmTestPhasePreparation = {
integrationTestPhasePreparation()
}
cassandraOrmTestPhaseCleanUp = {
integrationTestPhaseCleanUp()
}
eventTestPhasesStart = { phases ->
def junitTestType = loadClass('org.codehaus.groovy.grails.test.junit4.JUnit4GrailsTestType')
def spockTestType = loadClass('grails.plugin.spock.test.GrailsSpecTestType')
[cassandra: cassandraTests].each { name, types ->
if (!types.any { it.class == spockTestType }) {
types << spockTestType.newInstance(cassandraTestTypeName, cassandraTestTypeName)
}
}
[cassandraOrm: cassandraOrmTests].each { name, types ->
if (!types.any { it.class == junitTestType }) {
types << junitTestType.newInstance(cassandraOrmTestTypeName, cassandraOrmTestTypeName)
}
}
}
// Soft load classes
// http://jira.grails.org/browse/GRAILS-6453
// http://grails.1312388.n4.nabble.com/plugin-classes-not-included-in-classpath-for-plugin-scripts-td2271962.html
loadClass = { className ->
def load = { name ->
classLoader.loadClass(name)
}
try {
load(className)
} catch (ClassNotFoundException e) {
compile()
load(className)
}
}
@staticnull
Copy link
Author

I mostly followed Luke's blog when I got this working back in Grails 2.2.4: http://ldaley.com/post/615966534/custom-grails-test

We had the following directory structure under appName.
appName
  +--grails-app
    +--cassandra
    +--conf
    +--controllers
    +--domain (empty)
    ...
  +--test
    +--cassandra
    +--cassandraOrm
    +--integration
    +--unit

FYI- We were using the cassandra-orm plugin and our domain objects under test were in /grails-app/cassandra.

In BuildConfig we had the following:

dependencies {
    test "org.spockframework:spock-grails-support:0.7-groovy-2.0"
    test "org.codehaus.groovy.modules.http-builder:http-builder:0.5.2"
    compile 'org.codehaus.gpars:gpars:1.0.0'
}

plugins {
    runtime ":hibernate:$grailsVersion"
    runtime ":jquery:1.8.3"
    runtime ':resources:1.2'

    // Data Store
    compile(":cassandra-astyanax:0.4.2") {
        excludes "commons-logging"
        excludes "slf4j-log4j12"
    }
    compile ":cassandra-orm:0.4.2"

    // Testing
    compile ":codenarc:0.19"
    compile ":functional-spock:0.6", {
        excludes "spock-grails-support"
    }
    test(":spock:0.7") {
        exclude "spock-grails-support"
    }
    build ":tomcat:$grailsVersion"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment