I’ve created a Grails plugin which adds a custom test type class (extending GrailsTestTypeSupport) and custom test result class (extending GrailsTestTypeResult) to support a custom test type that I run during the other phase of the test-app script. Testing this on my local machine has gone swimmingly but…
When I packaged the plugin to use in my app, the tests are blowing up on our CI server (Jenkins). Here’s the error that Jenkins is spitting out:
unable to resolve class CustomTestResult @ line 58, column 9.
new CustomTestResult(tests.size() - failed, failed)
It appears that I cannot simply import these classes into _Events.groovy, and the classes are not otherwise on the classpath. But I’ll be damned if I can figure out how to get them onto the classpath. Here’s what I have so far (in _Events.groovy):
import java.lang.reflect.Constructor
eventAllTestsStart = {
if (!otherTests) otherTests = []
loadCustomTestResult()
otherTests << createCustomTestType()
}
private def createCustomTestType(String name = 'js', String relativeSourcePath = 'js') {
ClassLoader parent = getClass().getClassLoader()
GroovyClassLoader loader = new GroovyClassLoader(parent)
Class customTestTypeClass = loader.parseClass(new File("${customTestPluginDir}/src/groovy/custom/test/CustomTestType.groovy"))
Constructor customTestTypeConstructor = customTestTypeClass.getConstructor(String, String)
def customTestType = customTestTypeConstructor.newInstance(name, relativeSourcePath)
customTestType
}
private def loadCustomTestResult() {
ClassLoader parent = getClass().getClassLoader()
GroovyClassLoader loader = new GroovyClassLoader(parent)
Class customTestResultClass = loader.parseClass(new File("${customTestPluginDir}/src/groovy/custom/test/CustomTestResult.groovy"))
}
Currently: CustomTestResult is only referenced from within CustomTestType. As far as I can tell, _Events.groovy is loading CustomTestType but it is failing because it then insists that CustomTestResult is not on the classpath.
Putting aside for a moment that it seems crazy that there’s this much overhead to get plugin-furnished classes onto the classpath for the test cycle to begin with… I’m not quite sure where I’ve gotten tripped up. Any help or pointers would be greatly appreciated.
@Ian Roberts’ answer got me pointed in roughly the right direction, and combined with the
_Events.groovyscript from thisgrails-cucumberplugin, I managed to come through with this solution:First,
_Events.groovybecame this:Which is far more readable than where I was at the start of this thread. But: I was in roughly the same position: my
ClassNotFoundExceptionmoved from being thrown in_Events.groovyto being thrown from withinCustomTestTypewhen it tried to create an instance ofcustom.test. CustomTestResult. So withinCustomTestType, I added the following method:So Ian was right, inasmuch as
classLoadercame to the rescue — I just wound up needing its magic in two places.