简体   繁体   English

使用Spock存根@Immutable groovy类

[英]Stubbing @Immutable groovy classes using Spock

For instance I have a class 例如我有一堂课

@Immutable
class User {
    String id
}

and I want to stub it in the way I used to stub java classes 我想以以前的Java类存根方式存根

def "should stub class"() {
    given:
    def validator = new BitSolutionValidator(eventBus)
    def student = GroovyStub(User)

    when:
    validator.onStudentHandedOverBitSolution(new StudentHandedOverBitSolutionEvent(
            student, GroovyStub(BitTask), GroovyStub(BitSolution)))

    then:
    student.id >> STUDENT_ID
}

But when the code executed I noticed student.id returned null. 但是当执行代码时,我注意到student.id返回null。 Is there a way to stub @Immutable groovy classes using Spock or I have to use metaClass? 有没有一种方法可以使用Spock来对@Immutable groovy类进行存根,还是必须使用metaClass?

我已经将grails版本升级到2.4.3,问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM