Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Mocking FileChannel

william1104

2008-04-23

Replies: Find Java Web Hosting

Author LoginPost Reply

Hi all,

I tried to mock a FileChannel in a testcase with following codes.

// ----------------
  final Mockery context = new Mockery() {
   {
    setImposteriser(ClassImposteriser.INSTANCE);
   }
  };

  final FileChannel fileChannel = context.mock(FileChannel.class);
  context.checking(new Expectations() {
   {
    one(fileChannel).isOpen();
    will(returnValue(true));

    one(fileChannel).close();
   }
  });
  NIOUtils.closeFileChannelQuietly(fileChannel);
  context.assertIsSatisfied();
// ----------------

When I ran the test case, I got the following exception:

================
java.lang.NullPointerException
 at
java.nio.channels.spi.AbstractInterruptibleChannel.close (AbstractInterruptibleChannel.java:93)
================

The implementation of fileChannel.close() is not wrapped by jMock.
Unfortunately, according to jMock's description:

=================
The ClassImposteriser creates mock instances without calling the constructor
of the mocked class. So classes with constructors that have arguments or
call overideable methods of the object can be safely mocked. However, the
ClassImposteriser cannot create mocks of final classes or mock final
methods.
=================

The mock object is created with "newInstance()" method. That means the
default constructor of the target class - "AbstractInterruptibleChannel()"
is not called, the the mock object is not in a "good" state for use.

I will appreciate if anyone can provide a suggestion on how to handle this
scenario.

Thanks and regards,
William
--
Sent from the jMock - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.