I have a simple class like
class SomeClass { def foo() { def bar= bar() switch( bar ) { return something based on format } } def bar() { return someValue } }
I have already written complete unit tests for bar() . Now I need to write unit tests for foo() , which is highly dependent on the use of the bar() method. I donโt want to duplicate the installation phase the way it was done for bar() , so I would like to mock it by simply returning the desired values.
I know that Groovy easily supports this type of "ridicule" by simply defining it as SomeClass.property = "Hello World" . In addition, this can be done with employees like SomeClass.service = myMockedService . But I did not find a way to do this using the methods inside the device under test. There is?
I tried with MockFor , as in
def uut = new MockFor( SomeClass ) uut.demand.bar{ /* some values */ } uut.use { assert uut.foo() == expected }
but it gives me
groovy.lang.MissingMethodException: No signature of method: groovy.mock.interceptor.MockFor.foo() is applicable for argument types: () values: []
UPDATE
In fact, I came up with a simple solution for using a subclass. In the testing method, I create a subclass of SomeClass , where I override the method that I want to mock / stub. After that, using an instance of the subclass gives me what I need.
It seems a little rude. Any other suggestions on this?
source share