class A {
String a
}
class B extends A {
String b
}
现在,我想在创建的实例时通过地图构造设置这两个属性 B
def instance = new B(a: "foo", b: "bar")
assert instance.b != null
只有它不起作用。
实际上,它确实可以在纯Groovy中工作,但不适用于Spock测试中的Grails域对象。
以下测试通过了Grails 2.3.8。
超级班...
// grails-app/domain/inheritedproperties/SuperClass.groovy
package inheritedproperties
class SuperClass {
String a
}
一个子类...
// grails-app/domain/inheritedproperties/SubClass.groovy
package inheritedproperties
class SubClass extends SuperClass {
String b
}
Spock规格...
// test/unit/inheritedproperties/SubClassSpec.groovy
package inheritedproperties
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(SubClass)
@Mock(SuperClass)
class SubClassSpec extends Specification {
void "test binding inherited properties"() {
when:
def instance = new SubClass(a: 'A', b: 'B')
then:
'A' == instance.a
'B' == instance.b
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句