Created
November 26, 2014 19:45
-
-
Save staticnull/67cc573aaa2d9c19f5cc to your computer and use it in GitHub Desktop.
Grails 2.2.4 - Custom test phase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: