mardi 9 août 2016

Groovy metaclass, mocking a service method that has throws clause

I have an abstract service:

abstract class ParentService {    
    abstract Map someMethod() throws NumberFormatException    
}

And another service that extends above class:

class ChildService extends ParentService {
    @Override        
    Map someMethod() throws NumberFormatException {
        //Business Logic
    }
}

I want to mock someMethod() using groovy metaClass. I need to mock this method for writing test cases for ChildService. This is what I have done for mocking:

ChildService.metaClass.someMethod = { -> "Mocked method" }
But this is not working and the call always executes actual method from the service. What needs to be done here? Am I missing something?
Please Note that I have to mock just one method not the entire service.

Aucun commentaire:

Enregistrer un commentaire