mocking an object with final methods

6 views
Skip to first unread message

david

unread,
Apr 13, 2009, 6:33:25 PM4/13/09
to gmock-user
Hi,
I'm trying to mock the java.nio.ByteBuffer class. Just as a quick
example:

// test code
buf = gmc.mock(ByteBuffer)
gmc.ordered {
buf.get().returns(0x1e) // this line works
buf.position().returns(1) // this line does not
buf.capacity().returns(1) // this line does not
}

// code under test
byte b = bb.get();
System.out.println(b);
System.out.println(bb.position());
System.out.println(bb.capacity());

// output
30
0
0

The main difference I see between the 3 methods are that the latter 2,
position and capacity, are final. Is it possible to mock these calls
with gmock?

Thanks,
David

Johnny

unread,
Apr 13, 2009, 9:41:21 PM4/13/09
to gmock...@googlegroups.com
Hi David,

Unfortunately, gmock cannot mock final methods or final classes on the
Java side (but can do so on the Groovy side), neither do easymock and
jmock.

Johnny

在 2009-04-13一的 15:33 -0700,david写道:

Julien Gagnet

unread,
Apr 14, 2009, 2:31:43 AM4/14/09
to gmock...@googlegroups.com
Johnny is right about about final classes. However if you can live
without the strong typing it should work:

def buf = gmc.mock()
gmc.ordered {
buf.get().returns(0x1e) // this line works
buf.position().returns(1) // this line does not
buf.capacity().returns(1) // this line does not
}


2009/4/14 Johnny <johnny...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages