2

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.

UjjawalG
  • 21
  • 1

1 Answers1

0

Maybe you could just mock method on instance?

def child = new ChildService()
child.metaClass.someMethod = { -> "Mocked method" }
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • I tried to reproduce it and it was working, so reason why it's not working must be somewhere else. Can you show your whole test method? You can also try to change "Mocked method" to map. Maybe it's not working because of return type mismatch? – Krzysztof Atłasik Aug 10 '16 at 07:43