开发者

In Groovy, When Does It Makes Sense To Use An Expando vs the 'as' operator and Closures?

开发者 https://www.devze.com 2023-02-05 12:46 出处:网络
Groovy is a wonderful language that offers lots of different choices. In thinking ab开发者_开发知识库out unit tests, when does it make sense to use an Expando

Groovy is a wonderful language that offers lots of different choices.

In thinking ab开发者_开发知识库out unit tests, when does it make sense to use an Expando object vs. the "as" operator with closures?

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.

Thanks!


As noted on the page you referenced regarding using Closures instead of mocks, by themselves they only work for mocking interfaces and only if that interface has a single method. So if the class under test is not using an interface or you need to mock more than a single method you need to use either an Expando or Map. I prefer to be consistent and always use a Map, the code I deal with rarely needs objects mocked with a single interface method and using a Map does not require that many more keystrokes.

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)

Not sure there is a significant difference between using a Map or Expando to mock an object, but I prefer a Map just because of the nice way you can declare a Map literal and not have to new up an Expando.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号