vendredi 4 mars 2016

how to test grails 3 controller with services that have another services inject into them?

In unit test cases of controllers i'm only able to inject the services that are used in the corresponding controller.But if say one service in controller injects another service then my test cases gets failed i.e. can not invoke service method on null object.

@TestFor(MyController)
@Mock(MyService)
class MyControllerSpec extends Specification {
    void "test something"(){
        when:
            controller.method1();
        then:
            //something
    }

}

class MyController(){
    MyService myService

    void method1(){
        myService.serviceMethod()       
    }   
}

class MyService(){
    AnotherService anotherService
    void serviceMethod(){
        anotherService.anotherServiceMethod()
    }
}

class AnotherService(){
    void anotherServiceMethod(){
    \\something
    }
}

in this case, I'm getting can not invoke "anotherServiceMethod" on null object. is there any way to test this type of controller? and is it a good approach to inject a service in another service?

Aucun commentaire:

Enregistrer un commentaire