当前位置: 首页 > 知识库问答 >
问题:

Grails编译但Groovy失败

郎刚捷
2023-03-14

我在 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'
}
}

我可以知道为什么会有差异吗?谢谢。

共有1个答案

陶高峯
2023-03-14

在 Grails 中,数据绑定在构造域类时生效。

因此不会抛出异常,但如果您在域实例上看到errors属性,您将在那里看到一些消息。

然而,如果域类在您的示例中没有定义属性(如'namenot'),它将简单地忽略它。

 类似资料:
  • 问题内容: 我在主软件包的一个目录下有一些文件: main.go config.go server.go 当我这样做时:“执行构建”程序将完美构建并运行良好。当我这样做时:“ go run main.go”失败了。 输出: 未定义的符号是结构,并且大写,因此应将其导出。 我的Go版本:go1.1.2 linux / amd64 问题答案: 这应该工作 Go run需要一个文件或多个文件,并且它仅合

  • Groovy/Grails Tool Suite (GGTS)是一个不带所有插件的较小版本(但是你可以手动安装它们),但它会为Groovy和Grails的开发进行预配置:使用Groovy 1.8的Groovy-Eclipse、Grails IDE、tc Server integration和针对tc Server Developer Edition 2.7.0和Grails 2.0.4的运行时。

  • 这是我的Maven命令 对于这个项目 但我面对的是 [错误]没有更多标记-无法分析错误消息:Groovy:无法解析类委托,无法找到用于注释的类[错误]12。D:\Projects\lombok groovy example master\src\main\groovy\prystasj\lombok\example\groovy\Rocket中出错。groovy(第5行)[错误]@数据 mvn—版

  • 我如何在我的Grails Spock测试中实现Groovy的新特性?每次我尝试,我都会得到一个看起来像这样的堆栈跟踪。Groovy路径是否有一些我可能不知道的限制? JDK 版本: Groovy Verison: Grails版本: 简化代码: } 堆栈跟踪:

  • 我试图在eclipse中用maven构建一个spark应用程序,但我遇到了一个编译失败。我修正了系统变量,给它提供到JDK的路径,而不是到JRE的路径,如下所示: 并且我将这个字符串添加到变量中,但是没有起作用。因此,我尝试不使用如下但没有新内容。 因此,我尝试通过eclipse来指出我的JDK,如下所示:,我也选择了JDK,但这个解决方案也不起作用,所以我决定使用cmd来修复它: 我检查了,结果

  • 2017-05-23T15:14:03.19+0000[STG/17]出---->下载的应用程序包(59M)2017-05-23T15:14:03.36+0000[STG/0]出站失败:Buildpack编译步骤失败2017-05-23T15:14:03.36+0000[STG/0]错误2017-05-23T15:14:03.46+0000[API/0]错误遇到错误:应用程序在Buildpack编