In Groovy, when does it make sense to use Expando against the "as" operator and close?

Groovy is a great language with many different options.

Thinking about unit tests, when does it make sense to use an Expando object against a "like" operator with closure?

http://groovy.codehaus.org/Developer+Testing+using+Maps+and+Expandos+instead+of+Mocks vs http://groovy.codehaus.org/Developer+Testing+using+Closures+instead+of+ Mocks

For simple cases, their use seems so similar.

Thank!

+3
source share
1 answer

, Closures mocks, . , , , Expando Map. , , , , , , .

interface Foo {
    def someMethod(s)
}

// Closure, this breaks if someOtherMethod() is added to Foo or if Foo is a class
def mockMethod = { arg -> ...}
def myTestObject = new ObjectUnderTest(mockMetod as Foo)

// Map
def mockMethod = { arg -> ...}
def myTestObject = new ObjectUnderTest([someMethod:mockMethod] as Foo)

, Map Expando , - , Expando.

+4

Source: https://habr.com/ru/post/1785828/


All Articles