I’m trying to unit test a service and I would like to use a mock to override a method on domain object which retrieves a file from a DB.
def mockElem = mockFor(DataElement, false)
mockElem.demand.getFile(){return tempFile}
def dataElem = mockElem.createMock()
dataElem.orderId = "123"
dataElem.id = tempFileName
dataElem.dataType = "cnv"
dataElem.dataStatus = DataStatus.TRANSFERED
mockDomain(DataElement, [dataElem])
When I call a dynamic finder on the data element I want this Mock Domain to be returned with the mockFor demand functionality for getFile. An Assertion error is thrown when the MockDomain line is reached
junit.framework.AssertionFailedError: No call to ‘getClass’ expected
at this point. Still 1 call(s) to ‘getFile’ expected. at
groovy.mock.interceptor.StrictExpectation.match(StrictExpectation.groovy:56)
at grails.test.GrailsMock.createMock_closure1(GrailsMock.groovy:136)
at
grails.test.MockUtils.updateMetaClassForClass_closure95(MockUtils.groovy:1297)
at groovy.lang.Closure.call(Closure.java:412) at
groovy.lang.Closure.call(Closure.java:425) at
grails.test.MockUtils.updateMetaClassForClass(MockUtils.groovy:1294)
at grails.test.MockUtils.mockDomain(MockUtils.groovy:470) at
grails.plugin.spock.UnitSpec.mockDomain(UnitSpec.groovy:141) at
com.genospace.inbound.pg.HemeCNVPipelineTestSpec.test processing Heme
file(HemeCNVPipelineTestSpec.groovy:66)
Not sure what is the question.
You got this error because you are mocking an object twice :
mockFor/createMockmockDomainmockDomainneed to know some information about objects being passedas arguments (here it verifies the class is correct) butmockFordid not allow thatgetClass()was called : you did not add ademandfor such call.Do you really need to mock with
demand?I think the simple case should work :