jeudi 9 juillet 2015

How to mock a method which passes in clazz?

I'm trying to write a unit test for a method which has takes in a .class parameter.

For example:

ExampleService {
    def myExample(clazz){
        //do stuff and return some object
    }
}

/* How the above function gets used */
AnotherExampleService {
    exampleService

    def blah() {
        def obj = myExample(AnotherClass.class)
    }
}

/* Now I need to test the above blah() function */
AnotherExampleServiceSpec extends Specification {
    def "test blah"() {
        //The following doesn't seem to work
        ExampleService.metaClass.myExample = { def arg1 -> obj }
    }
}

I'm pretty new to Groovy/Grails, so any help would be much appreciated. I basically want to know why my unit test doesn't seem to work, and how to test such a function that takes in a paramter of a class instance.
Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire