I wanted to create a unit test verifying that a stream was closed. My class decorates the stream it receives with DataputStream, which seems to break the mocking demands feature.
void testBadMock() {
def mockInputClass = new MockFor(InputStream)
mockInputClass.demand.with {
close() {}
}
def mockInput1 = mockInputClass.proxyInstance()
mockInput1.close()
mockInputClass.verify mockInput1 // passes
def mockInput2 = mockInputClass.proxyInstance()
new DataInputStream(mockInput2).close()
mockInputClass.verify mockInput2 // fails
}
I checked the source code for DataInputStream, and as expected, the stream passed into the constructor is the object which it delegates the close() method call to.
def fakeInput = [close: {println 'I was called'}] as InputStream
new DataInputStream(fakeInput).close() // prints 'I was called'
I see no reason why my mock object is not seeing the close() method call.
Aucun commentaire:
Enregistrer un commentaire