unit testing - How to mock a method which passes in clazz? -


i'm trying write unit test method has takes in .class parameter.

for example:

exampleservice {     def myexample(clazz){         //do stuff , return object     } }  /* how above function gets used */ anotherexampleservice extends exampleservice {       def blah() {         def obj = myexample(anotherclass.class)     } }  /* need test above blah() function */ anotherexampleservicespec extends specification {     def "test blah"() {         //the following doesn't seem work         exampleservice.metaclass.myexample = { def arg1 -> obj }     } } 

i'm pretty new groovy/grails, appreciated. want know why unit test doesn't seem work, , how test such function takes in paramter of class instance.
thank help!

i subclass exampleservice in test. looked like:

exampleservice {     def myexample(clazz){         //do stuff , return object     } }  anotherexampleservice extends exampleservice {     def exampleservice      def blah() {         def obj = myexample(anotherclass)     } }  anotherexampleservicespec extends specification {     def "test blah"() {         given:            anotherexampleservice service = new anotherexampleservice() {               @override               def myexample(clazz){                  //whatever want                  return whateverresult                 }         when:            def result = service.blah (someclass)         then:             result     } } 

as can see myexample methods overridden here return mock value , there's no tinkering metaclass :-) make easier groovy can explicitly state input type e.g. def myexample(class clazz).


Comments