【问题标题】:Grails cant use Spock framework?Grails不能使用Spock框架?
【发布时间】:2013-07-02 10:33:01
【问题描述】:

我目前正在处理一个没有 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)

【问题讨论】:

    标签: testing grails spock


    【解决方案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 并重试。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        相关资源
        最近更新 更多