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.metaClass.constructor is something I found at:
http://manuel-palacio.blogspot.com/2010/07/groovy-tip-metaprogramming-1.html
Thanks!
groovy.sql.Sqlhas 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 syntaxnew Sql(connection: connection)?.I’m kind of surprised, though, that that technique for stubbing doesn’t generate an exception, e.g., when running
sql.executeor the like.