【发布时间】:2014-02-21 14:14:47
【问题描述】:
我在 Groovy 控制台中测试了以下代码,但都按预期失败: 第一次测试:
class TekEvent {
Date organizer
}
def tekEvent = new TekEvent(organizer: 'John Doe')
assert tekEvent.organizer == 'John Doe'
Exception thrown
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'John Doe' with class 'java.lang.String' to class 'java.util.Date'
第二次测试:
class DifferentTekEvent {
String name
}
def tekEvent = new TekEvent(nameNot: 'John Doe')
assert tekEvent.nameNot == 'John Doe'
Exception thrown
groovy.lang.MissingPropertyException: No such property: nameNot for class: DifferentTekEvent
等价物在 Grails 中运行(即类被实例化并在单元测试中使用)但不会抛出异常。例如:
@TestFor(TekEvent)
class TekEventSpec extends Specification {
void "test"() {
def tekEvent = new TekEvent(organizer: 'aasdf') //no expceptions thrown here. Why?
expect:
tekEvent.organizer == 'aasdf'
}
}
我可以知道为什么会有差异吗?谢谢。
【问题讨论】: