我创建了一个 Grails 插件,它添加了自定义测试类型类(扩展 GrailsTestTypeSupport
)和自定义测试结果类(扩展 GrailsTestTypeResult
)以支持自定义测试类型我在 test-app
脚本的 other
阶段运行。在我的本地机器上进行测试进展顺利,但是......
当我打包插件以在我的应用程序中使用时,测试在我们的 CI 服务器(Jenkins)上崩溃了。这是 Jenkins 吐出的错误:
unable to resolve class CustomTestResult @ line 58, column 9.
new CustomTestResult(tests.size() - failed, failed)
看来我不能简单地将这些类导入
到_Events.groovy
中,并且这些类不在类路径上。但如果我能弄清楚如何让它们进入类路径,那我就该死了。这是我到目前为止所拥有的(在 _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"))
}
目前:CustomTestResult
仅在 CustomTestType
中引用。据我所知,_Events.groovy
正在加载CustomTestType
,但它失败了,因为它坚持认为CustomTestResult
不在类路径上。
暂时搁置一下,将插件提供的类放入类路径以开始测试周期似乎很疯狂......我不太确定我在哪里被绊倒了。任何帮助或指示将不胜感激。
最佳答案
您是否尝试过通过 ClassLoader 简单地加载相关类,该类加载器可通过 _Events.groovy
中的 classLoader
变量访问?
Class customTestTypeClass = classLoader.loadClass('custom.test.CustomTestType')
// use nice groovy overloading of Class.newInstance
return customTestTypeClass.newInstance(name, relativeSourcePath)
您应该在 eventAllTestsStart
的流程中迟到足够的时间才能使此有效。
关于Grails:将插件类导入到 _Events.groovy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328510/