1

我目前正在从事一个没有 TDD 的项目,现在我已采取以下步骤开始在该项目上使用 Spock 进行单元和集成测试:

  1. 在buildconfig中添加了以下内容:

    测试“:spock:0.7”

  2. 然后在“test/unit/MYCLASSNAME”创建了一个名为MyfunctionControllerSpec的规范,如下所示:

    import grails.test.mixin.*
    import spock.lang.Specification
    
    class MyfunctionControllerSpec extends Specification {
    
        void "list() should return no results with no records in DB"() {
            given:
                def model = controller.list()
            expect:
                model.taskInstanceList.size() == 0
                model.taskInstanceTotal == 0
        }
    
    }
    

但是,规范导入行出现以下错误:

Groovy:unable to resolve class spock.lang.Specification

我不明白我做错了什么,我是否导入或安装了 Spock 错误?

提前致谢


编辑*


我尝试了下面的建议,然后解决方案无法运行,它仍然无法识别规范类,即使当我开始输入“inport spo”并按 cntrl+space 时也没有出现,就好像它甚至无法识别插件一样:

Loading Grails 2.1.0
| Configuring classpath
| Downloading: spock-grails-support-0.7-groovy-2.0.pom.sha1
| Downloading: spock-core-0.7-groovy-2.0.pom.sha1
| Downloading: spock-grails-support-0.7-groovy-2.0.jar.sha1
| Downloading: spock-core-0.7-groovy-2.0.jar.sha1.
| Environment set to development....
| Error Error loading event script from file [/media/system/workspace/sms_bskyb_New_V2(Dynam Messages)/plugins/tool-ui/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
| Error Error loading event script from file [/home/system/.grails/2.1.0/projects/sms_bskyb/plugins/database-migration-1.1/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
| Error Error loading event script from file [/home/system/.grails/2.1.0/projects/sms_bskyb/plugins/tomcat-2.1.0/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
| Error Error loading event script from file [/home/system/.grails/2.1.0/projects/sms_bskyb/plugins/spock-0.7/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
| Error Error loading event script from file [/home/system/.grails/2.1.0/projects/sms_bskyb/plugins/webxml-1.4.1/scripts/_Events.groovy] startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
| Environment set to development.....
| Packaging Grails application.
| Error Fatal error during compilation org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/system/.grails/ivy-cache/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.reflect.InvocationTargetException
1 error
 (Use --stacktrace to see the full trace)
4

2 回答 2

1

您使用的是 Grails 2.1.0,而 Groovy 2.0 是在 Grails 2.2.0 及更高版本中引入的。希望您不需要显式依赖org.spockframework:spock-grails-support:0.7-groovy-2.0。仅使用如下:

plugins{
    test ":spock:0.7"
}

如果您仍然发现问题,则通过创建一个全新的裸骨 grails 应用程序来隔离问题,并按照插件文档中的说明安装插件。尝试查看是否有任何类路径冲突。如果问题仍然存在,则清除ivy-cache和/或.m2重试。

于 2013-07-02T13:53:15.317 回答
0

我几乎在这里回答了这个问题,但我看到您使用的是 Grails 2.1。

对于使用Grails 2.2.x遇到此错误的其他人,请参阅此答案

于 2013-10-11T14:11:23.693 回答