I have a service class method in my grails porject which uses a helper class to fetch the response as xml. The xml is then send out as the response of the service class method.
ServiceClass:-
class ItemService{
def getItem(Number) {
def HelperClass helper = new HelperClass()
def responseXml = helper.getMessage(Number)
return responseXml
}
}
I writing a test case for this method. In my test case, i wish to mock
def responseXml = helper.getMessage(Number)
The test case that i have written so far is:
class ItemServiceTest extends GroovyTestCase {
public final void testFindItem(){
def service = new ItemService()
def xml = "<Item><Number>123</Number></Item>"
def mockJobServiceFactory = new MockFor(HelperClass)
mockJobServiceFactory.demand.getMessage{ def str ->
return xml
}
service.getItem().HelperClass = mockJobServiceFactory.proxyInstance()
def item = service.getItem()("123")
assertNotNull(item)
}
but still, the mock does not return the xml that I speicified in the test case.
Any problems that you see in the above approach?…Advance Thanks
When you mock objects with
MockFor, you need to surround the code that uses the mock in auseclosure. Try this: