freepascal / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Add partial mocking #72

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
"Partial mocking" means that mock objects can be configured to call real
methods.

1. I already have the following implemented, will attach the patch to this
issue shortly:

when(mock.someMethod()).thenCallRealMethod()
or
doCallRealMethod().when(mock).someMethod()

2. Another use case is to create mock object that calls real methods by
default.  I don't have this working yet, but thinking of doing this:

mock = mock(MyClass.class, new CallRealMethods())

where CallRealMethdods implements ReturnValues.  The resulting mock object
will call real methods by default, unless the method is explicitly stubbed.

Original issue reported on code.google.com by amur...@gmail.com on 15 Apr 2009 at 5:12

GoogleCodeExporter commented 9 years ago
Patch files attached (sorry for 2 files, I'm new to SVN and for some reason it
refused to make one patch at the root).  Let me know if there are any problems 
or
questions.

Original comment by amur...@gmail.com on 17 Apr 2009 at 7:41

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks, I'm looking at it

Original comment by szcze...@gmail.com on 20 Apr 2009 at 10:16

GoogleCodeExporter commented 9 years ago
It's checked in to trunk. I did some minor changes but essentially all went to 
trunk.

Some things might change, though. Before the release we might get rid of
CALLS_REAL_METHODS because we want the spy() method to do this (because we would
rather have single method to do the same thing). Obviously this require some 
work on
spy() method...

There is one thing you have to be aware of regarding your implementation. Mock
objects are instantiated by Objenesis and therefore their state is messed up 
(e.g.
fields are not initiated). If you doCallRealMethod() on a method that depends 
on the
state of the object (e.g. uses a field) then you will stop smiling :)

Original comment by szcze...@gmail.com on 21 Apr 2009 at 7:20

GoogleCodeExporter commented 9 years ago
Thanks!  When are you planning the next release?

Yes, I've already encountered your point about the object state.  It's not a 
huge
problem because the internal fields are still settable, you just need to call 
the
real setters (which can be done now!)  Alternatively, you can use reflection to
inject the values (see InjectionUtils from Unitils).  This isn't pretty, but I'm
working with some really ugly legacy code here in the real world :)

Original comment by amur...@gmail.com on 21 Apr 2009 at 7:53

GoogleCodeExporter commented 9 years ago
Hi,

I'm about to hide Mockito.CALLS_REAL_METHODS because I changed the code behind 
the
spy() so that the spy actually does it. Does it make sense?

Original comment by szcze...@gmail.com on 12 May 2009 at 3:10

GoogleCodeExporter commented 9 years ago
Could you post the API or code fragment to clarify what you are planning to do? 
 Thanks!

Original comment by amur...@gmail.com on 12 May 2009 at 5:30

GoogleCodeExporter commented 9 years ago
Sure,

Instead of:

mock(Foo.class, CALLS_REAL_METHODS);

You write:

spy(fooInstance);

Original comment by szcze...@gmail.com on 12 May 2009 at 5:44

GoogleCodeExporter commented 9 years ago
:) thanks.

Here's what the doc says now:
 Spying on real objects is often associated with "partial mocking" concept. However,
Mockito spies are not partial mocks. Mockito spy is meant to help testing other
classes - not the spy itself. Therefore spy will not help if you intend to 
verify if
method calls other method on the same object. In this case I suggest being 
OO/SRPy
(for example you might extract new class/interface...)

So... what will be the change in spy behavior that will make it usable as a 
partial mock?

Will I be able to do this, for example:

// setup
mySpy = spy(fooInstance);
when(mySpy.getValue()).thenReturn(47);

// test doSomething()
// this should NOT call the real getValue
mySpy.doSomething();

verify(mySpy).getValue();

Original comment by amur...@gmail.com on 12 May 2009 at 6:47

GoogleCodeExporter commented 9 years ago
yup, the docs are somewhat deprecated according to my last change to spy()

Pretty much you got that right with the example if doSomething() calls
this.getValue() internally.

Original comment by szcze...@gmail.com on 12 May 2009 at 7:14

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 9 Jun 2009 at 3:15