开发者

Why Does This Groovy MetaClass Statement Work with Sql class?

Why does this line of unit test code work? groovy.sql.Sql doesn't have a no argument constructor.

Sql.metaClass.constructor = { dataSource -> return new Sql(); }

That line is amongst some others in a grails app which mocks out a Sql object's constructor and one of its methods. It works great.

Looking at the API for the Sql object, I do not see a no argument constructor: http://groovy.codehaus.org/api/groovy/sql/Sql.html

This style of overriding the constructor using Sql.metaC开发者_如何学编程lass.constructor is something I found at: http://manuel-palacio.blogspot.com/2010/07/groovy-tip-metaprogramming-1.html

Thanks!


groovy.sql.Sql has no public no-args constructor, but as can be seen in the source, it does have a private no-args constructor -- I guess in order to support the syntax new Sql(connection: connection)?.

I'm kind of surprised, though, that that technique for stubbing doesn't generate an exception, e.g., when running sql.execute or the like.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜