user3240644
user3240644

Reputation: 2281

Grails compiles but Groovy fails

I tested the following code in the Groovy Console and both fails as expected: The first test:

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'

The second test:

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

The equivalents are run in Grails (i.e. the classes are instantiated and used in the unit tests) but the exceptions are not thrown. for example:

@TestFor(TekEvent)
class TekEventSpec extends Specification {

void "test"() {

    def tekEvent = new TekEvent(organizer: 'aasdf') //no expceptions thrown here. Why?
    expect:
        tekEvent.organizer == 'aasdf'
}
}

May I know why is there is discrepancy? Thanks.

Upvotes: 1

Views: 94

Answers (1)

aldrin
aldrin

Reputation: 4572

In Grails, the Data binding takes effect when you construct a domain class.

So no exceptions are thrown, but if you see the errors property on the domain instance, you will see some messages there.

afaik, if the domain class doesn't have a property defined (like 'nameNot') in your example, it will simply ignore that.

Upvotes: 2

Related Questions