-Re: Mocking classes for unit tests Was: asynchbase-1.2.0-rc1 is available for download
I put forward rolling in Powermock a while back, but it always seems a bit
overkill for just one or two methods. However, if we make using powermock a
'best practice' going forward, then it becomes reasonable.
Powermock is really nice in that not only can you access private methods
and mock final objects, you can always set private fields and move out
builtin in classes (eg. System, Integer, etc). Pretty sweet.
Only long standing problem I've found with it is that, by necessity, almost
everything is done via reflection, so method names to (power)mock need to
be passed as strings, which can be annoying (but not hard) to maintain.
+1 on adding in powermock as a best practice and updating the book to say
Just my $0.02
On Wed, Feb 22, 2012 at 9:23 AM, Ted Yu <[EMAIL PROTECTED]> wrote:
> Benoit's comment is directly related to our practice, especially when we
> add new methods which are only called by unit tests.
> ---------- Forwarded message ----------
> From: tsuna <[EMAIL PROTECTED]>
> Date: Wed, Feb 22, 2012 at 9:13 AM
> Subject: Re: asynchbase-1.2.0-rc1 is available for download
> To: Jim Scott <[EMAIL PROTECTED]>
> Cc: AsyncHBase <[EMAIL PROTECTED]>
> On Wed, Feb 22, 2012 at 8:50 AM, Jim Scott <[EMAIL PROTECTED]> wrote:
> > I ask because I am writing some unit tests to make sure that my code is
> > working properly and I cannot mock the HBaseClient because it is final. I
> > completely understand the rationale to make it final, but without it
> > implement an interface or being non-final I cannot mock the object.
> Hi Jim,
> OpenTSDB has unit tests that are mocking out HBaseClient just fine
> . You can mock out pretty much anything on the JVM: final,
> private, JDK stuff, etc. All you need is the right tools. I've been
> very happy with PowerMock. It supports Mockito and EasyMock.
> I've never been keen on mutilating public interfaces for the sake of
> testing. With tools like PowerMock, we can keep the public APIs tidy
> while mocking and overriding anything, even in the most private guts
> of the classes.
> Benoit "tsuna" Sigoure
> Software Engineer @ www.StumbleUpon.com