I'm throwing another potential solution: make default stubs return
mocks instead of nulls. This option can be spiked with a help of
experimental configuration API:
http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/configuration/experimental/ConfigurationSupport.html
You can try it and tell us how it works.
Also: tell, don't ask :-)
Szczepan Faber
On Tue, Sep 23, 2008 at 8:13 PM, Dan North <tast...@gmail.com> wrote:
For above one you might need a decent 'expect' api... I wonder where
is it going... Single library with both spying and mocking API? :)
I started thinking about the case you're coming from:
>It's not immediately obvious where this NPE came from - which is one of the risks of verify-afterwards.
I'm inclined to disagree - it's pretty obvious where the NPE comes
from. After all, I added this dependency + the logic that works on the
return value. Unless the code is really big & sucky :) For me, your
case is more like a proof that "tell don't ask" is a neat practice.
I'm really interested in the results of your spikes!
Cheers,
Szczepan Faber
> Cowshed cowshed4 = mock(Cowshed.class, STRICT); // throws exception on anyFor above one you might need a decent 'expect' api... I wonder where
> unstubbed call
is it going... Single library with both spying and mocking API? :)
>It's not immediately obvious where this NPE came from - which is one of the risks of verify-afterwards.I'm inclined to disagree - it's pretty obvious where the NPE comes
from.
After all, I added this dependency + the logic that works on the
return value. Unless the code is really big & sucky :) For me, your
case is more like a proof that "tell don't ask" is a neat practice.
I'm really interested in the results of your spikes!
Cheers,
Szczepan Faber
Hmmm, very interesting. I'm dying to know about cases of people going
back classical mocking and the reasons behind it. Make sure you get
his story before you burn the heretic... :) Seriously though, let us
know what happened eventually and if he is really happy with easymock.
>strict, smart and normal
I like the idea. You think different styles would be applicable for
different applications? E.g. new code => normal, legacy code =>
strict/smart?
> I'll keep you posted. So when are you migrating mockito to a mercurial repository? ;)
Yeah, I've gotta do it. I'm so tired with svn. I moved mockito-python
to hg and I smile very often now. I would still like to have some
syncing process (hg->svn) because I don't want to lose potential
contributors (java guys are so used to svn).
cheers,
Szczepan Faber
How long did the convert take? I'm wondering how fast is hg convert.
cheers,
Szczepan
Szczepan Faber
Szczepan
Cheers,
Igor
1. I'm slowly thinking about making SMART_NULLS a default behavior of any mock.
2. Another thing to discuss is how to approach collections - return
empty collections or SMART_NULLS (I'm voting for empty collections).
3. Does it make sense to keep variants like SMART_NULLS / MOCKS /
NULLS if the default behavior will be SMART_NULLS?
Cheers,
Szczepan Faber
I played with this a bit (not yet merged to trunk, though). I think
it's way cooler to get "Unstubbed method was invoked here" instead of
NPE.
1. I'm slowly thinking about making SMART_NULLS a default behavior of any mock.
2. Another thing to discuss is how to approach collections - return
empty collections or SMART_NULLS (I'm voting for empty collections).
3. Does it make sense to keep variants like SMART_NULLS / MOCKS /
NULLS if the default behavior will be SMART_NULLS?
Cheers,
Szczepan Faber
Cheers,
Szczepan Faber
I merged the smart nulls feature into the trunk.
It will change a bit,
for example it will use cglib to generate smart nulls, etc.
Hmmm, It complained when I tried with concrete class.
Nevermind though, I have a feeling that there is some proxy creation
logic in the codebase already :)
Cheers,
Szczepan
If a test invokes an unstubbed method on a mock object:
- If the method returns non-Collection throw an exception
- If the method returns a Collection then return a special Mockito
implementation that implements these rules (that is, throws an exception
if the method returns a non-collection itself)
Gili
Dan North wrote:
> Is it checked in as is? If so I'll pull down the latest on trunk and
> have a play.
>
> Cheers,
> Dan
>
> 2008/10/31 szczepiq <szcz...@gmail.com <mailto:szcz...@gmail.com>>
>
>
> >It already does! It uses the cglib Proxy & InvocationHandler so it
> looks just like java.lang.reflect but works on concrete classes.
>
> Hmmm, It complained when I tried with concrete class.
>
> Nevermind though, I have a feeling that there is some proxy creation
> logic in the codebase already :)
>
> Cheers,
> Szczepan
>
> On Fri, Oct 31, 2008 at 12:06 PM, Dan North <tast...@gmail.com
> <mailto:tast...@gmail.com>> wrote:
> > 2008/10/30 szczepiq <szcz...@gmail.com <mailto:szcz...@gmail.com>>
> <mailto:szcz...@gmail.com>>
> >> >>>>
> >> >>>> Cool, thanks for the info. I looked it up on the jaoo agenda
> >> >>>>
> (http://jaoo.dk/aarhus-2008/presentation/Test+Driven+Development).
> >> >>>> It's written there that you've used jmock when you actually
> have used
> >> >>>> mockito. Cheaters :)
> >> >>>>
> >> >>>> Szczepan
> >> >>>>
> >> >>>> On Wed, Oct 15, 2008 at 9:40 PM, Dan North
> <tast...@gmail.com <mailto:tast...@gmail.com>>
> >> >>>> wrote:
> >> >>>> > I'll try to put the changeset into the bitbucket repo as
> a named
> >> >>>> > branch.
> >> >>>> > Wish me luck :)
> >> >>>> >
> >> >>>> > Have fun with your presentation.
> >> >>>> >
> >> >>>> > Oh, and Erik Doernenburg and I ran a one-day TDD tutorial
> at JAOO
> >> >>>> > using
> >> >>>> > JUnit 4 and Mockito a couple of weeks ago which went
> really well.
> >> >>>> > They
> >> >>>> > totally got mocking and apparently that's unusual
> compared to the
> >> >>>> > other
> >> >>>> > times Erik has run the course. (This was the first time using
> >> >>>> > Mockito,
> >> >>>> > on
> >> >>>> > previous occasions he's used JMock.)
> >> >>>> >
> >> >>>> > Cheers,
> >> >>>> > Dan
> >> >>>> >
> >> >>>> >
> >> >>>> > 2008/10/15 szczepiq <szcz...@gmail.com
> <mailto:szcz...@gmail.com>>
> >> >>>> >>
> >> >>>> >> Cool! I'll definitely give it a go. I think I'm gonna
> just get it
> >> >>>> >> from
> >> >>>> >> bitbucket first. Just after I finish off the
> presentation for
> >> >>>> >> tomorrow
> >> >>>> >> JDD'08 :)
> >> >>>> >>
> >> >>>> >> Szczepan Faber
> >> >>>> >>
> >> >>>> >> On Wed, Oct 15, 2008 at 8:30 PM, Dan North
> <tast...@gmail.com <mailto:tast...@gmail.com>>
> <mailto:tast...@gmail.com>>
> >> >>>> >> >>
> >> >>>> >> >> It depends on the network connection. I left it running
> >> >>>> >> >> overnight
> >> >>>> >> >> (!)
> >> >>>> >> >> for
> >> >>>> >> >> the full thing. If you can get onto the actual server box
> >> >>>> >> >> apparently
> >> >>>> >> >> it's
> >> >>>> >> >> super-fast from a file:// repository. Over http it's
> not the
> >> >>>> >> >> fastest.
> >> >>>> >> >>
> >> >>>> >> >> Cheers,
> >> >>>> >> >> Dan
> >> >>>> >> >>
> >> >>>> >> >>
> >> >>>> >> >> 2008/9/29 szczepiq <szcz...@gmail.com
> <mailto:szcz...@gmail.com>>
> >> >>>> >> >>>
> >> >>>> >> >>> Ok, cool.
> >> >>>> >> >>>
> >> >>>> >> >>> How long did the convert take? I'm wondering how
> fast is hg
> >> >>>> >> >>> convert.
> >> >>>> >> >>>
> >> >>>> >> >>> cheers,
> >> >>>> >> >>> Szczepan
> >> >>>> >> >>>
> >> >>>> >> >>> On Mon, Sep 29, 2008 at 9:59 AM, Dan North
> >> >>>> >> >>> <tast...@gmail.com <mailto:tast...@gmail.com>>
> <mailto:robert...@gmail.com>>
> >> >>>> >> >>> >>
> >> >>>> >> >>> >> On 25 Sep, 15:35, szczepiq <szcze...@gmail.com
Existing functionality has not changed. There is an additional method
on Mockito class that allows you to create mock with different return
values strategy. The interface might change a bit.
Cheers,
Szczepan Faber
Gili
Cheers,
Szczepan Faber
How about this:
- returns primitive value
- returns "null object" of certain types, eg: empty collections,
zeros for Integers, etc.
- returns SmartNull
- returns null when SmartNull cannot be created
Dan, Per, anyone interested, what do you think? I can lay down my
reasoning but I want to know your opinions first.
Cheers,
Szczepan Faber
Gili
Gili
> <mailto:szcz...@gmail.com> <mailto:szcz...@gmail.com
> <mailto:szcz...@gmail.com>>>
> >>>>>
> >>>>>
> >>>>> >It already does! It uses the cglib Proxy &
> InvocationHandler so it
> >>>>> looks just like java.lang.reflect but works on concrete
> classes.
> >>>>>
> >>>>> Hmmm, It complained when I tried with concrete class.
> >>>>>
> >>>>> Nevermind though, I have a feeling that there is some
> proxy creation
> >>>>> logic in the codebase already :)
> >>>>>
> >>>>> Cheers,
> >>>>> Szczepan
> >>>>>
> >>>>> On Fri, Oct 31, 2008 at 12:06 PM, Dan North
> <tast...@gmail.com <mailto:tast...@gmail.com>
> >>>>> <mailto:tast...@gmail.com <mailto:tast...@gmail.com>>>
> wrote:
> >>>>> > 2008/10/30 szczepiq <szcz...@gmail.com
> <mailto:szcz...@gmail.com> <mailto:szcz...@gmail.com
> <mailto:szcz...@gmail.com>>>
> >>>>> >>
> >>>>> >> I merged the smart nulls feature into the trunk.
> >>>>> >
> >>>>> > Sweet :)
> >>>>> >
> >>>>> >>
> >>>>> >> It will change a bit,
> >>>>> >> for example it will use cglib to generate smart
> nulls, etc.
> >>>>> >
> >>>>> > It already does! It uses the cglib Proxy &
> InvocationHandler so
> >>>>> it looks
> >>>>> > just like java.lang.reflect but works on concrete classes.
> >>>>> >
> >>>>> > Cheers,
> >>>>> > Dan
> >>>>> >
> >>>>> >
> >>>>> >> Cheers,
> >>>>> >> Szczepan Faber
> >>>>> >>
> >>>>> >> On Sat, Oct 18, 2008 at 10:49 PM, szczepiq
> <szcz...@gmail.com <mailto:szcz...@gmail.com>
> >>>>> <mailto:szcz...@gmail.com <mailto:szcz...@gmail.com>>>
> wrote:
> >>>>> >> > I played with this a bit (not yet merged to trunk,
> though). I
> >>>>> think
> >>>>> >> > it's way cooler to get "Unstubbed method was
> invoked here"
> >>>>> instead of
> >>>>> >> > NPE.
> >>>>> >> >
> >>>>> >> > 1. I'm slowly thinking about making SMART_NULLS a
> default
> >>>>> behavior of
> >>>>> >> > any mock.
> >>>>> >> > 2. Another thing to discuss is how to approach
> collections -
> >>>>> return
> >>>>> >> > empty collections or SMART_NULLS (I'm voting for empty
> >>>>> collections).
> >>>>> >> > 3. Does it make sense to keep variants like
> SMART_NULLS / MOCKS /
> >>>>> >> > NULLS if the default behavior will be SMART_NULLS?
> >>>>> >> >
> >>>>> >> > Cheers,
> >>>>> >> > Szczepan Faber
> >>>>> >> >
> >>>>> >> > On Thu, Oct 16, 2008 at 10:33 PM, Igor Czechowski
> >>>>> >> > <iczec...@gmail.com
> <mailto:iczec...@gmail.com> <mailto:iczec...@gmail.com
> <mailto:iczec...@gmail.com>>> wrote:
> >>>>> >> >>
> >>>>> >> >> I've uploaded the svn patch into Rietveld. The link is
> >>>>> >> >> http://codereview.appspot.com/7301
> >>>>> >> >>
> >>>>> >> >> Cheers,
> >>>>> >> >> Igor
> >>>>> >> >>
> >>>>> >> >> On Thu, Oct 16, 2008 at 10:54 AM, Dan North
> >>>>> <tast...@gmail.com <mailto:tast...@gmail.com>
> <mailto:tast...@gmail.com <mailto:tast...@gmail.com>>> wrote:
> >>>>> >> >>> It was a "late substitution". We decided during
> the main
> >>>>> conference
> >>>>> >> >>> that we
> >>>>> >> >>> should use JUnit 4 and Mockito because we think
> they are the
> >>>>> future of
> >>>>> >> >>> TDD
> >>>>> >> >>> and mocking in Java ;)
> >>>>> >> >>>
> >>>>> >> >>>
> >>>>> >> >>> 2008/10/15 szczepiq <szcz...@gmail.com
> <mailto:szcz...@gmail.com>
> >>>>> <mailto:szcz...@gmail.com <mailto:szcz...@gmail.com>>>
> >>>>> >> >>>>
> >>>>> >> >>>> Cool, thanks for the info. I looked it up on the
> jaoo agenda
> >>>>> >> >>>>
> >>>>>
> (http://jaoo.dk/aarhus-2008/presentation/Test+Driven+Development).
> >>>>> >> >>>> It's written there that you've used jmock when
> you actually
> >>>>> have used
> >>>>> >> >>>> mockito. Cheaters :)
> >>>>> >> >>>>
> >>>>> >> >>>> Szczepan
> >>>>> >> >>>>
> >>>>> >> >>>> On Wed, Oct 15, 2008 at 9:40 PM, Dan North
> >>>>> <tast...@gmail.com <mailto:tast...@gmail.com>
> <mailto:tast...@gmail.com <mailto:tast...@gmail.com>>>
> >>>>> <mailto:szcz...@gmail.com <mailto:szcz...@gmail.com>>>
> >>>>> >> >>>> >>
> >>>>> >> >>>> >> Cool! I'll definitely give it a go. I think
> I'm gonna
> >>>>> just get it
> >>>>> >> >>>> >> from
> >>>>> >> >>>> >> bitbucket first. Just after I finish off the
> >>>>> presentation for
> >>>>> >> >>>> >> tomorrow
> >>>>> >> >>>> >> JDD'08 :)
> >>>>> >> >>>> >>
> >>>>> >> >>>> >> Szczepan Faber
> >>>>> >> >>>> >>
> >>>>> >> >>>> >> On Wed, Oct 15, 2008 at 8:30 PM, Dan North
> >>>>> <tast...@gmail.com <mailto:tast...@gmail.com>
> <mailto:tast...@gmail.com <mailto:tast...@gmail.com>>>
> >>>>> <mailto:tast...@gmail.com <mailto:tast...@gmail.com>>>
> >>>>> >> >>>> >> >>
> >>>>> >> >>>> >> >> It depends on the network connection. I
> left it running
> >>>>> >> >>>> >> >> overnight
> >>>>> >> >>>> >> >> (!)
> >>>>> >> >>>> >> >> for
> >>>>> >> >>>> >> >> the full thing. If you can get onto the
> actual server box
> >>>>> >> >>>> >> >> apparently
> >>>>> >> >>>> >> >> it's
> >>>>> >> >>>> >> >> super-fast from a file:// repository. Over
> http it's
> >>>>> not the
> >>>>> >> >>>> >> >> fastest.
> >>>>> >> >>>> >> >>
> >>>>> >> >>>> >> >> Cheers,
> >>>>> >> >>>> >> >> Dan
> >>>>> >> >>>> >> >>
> >>>>> >> >>>> >> >>
> >>>>> >> >>>> >> >> 2008/9/29 szczepiq <szcz...@gmail.com
> <mailto:szcz...@gmail.com>
> >>>>> <mailto:szcz...@gmail.com <mailto:szcz...@gmail.com>>>
> >>>>> >> >>>> >> >>>
> >>>>> >> >>>> >> >>> Ok, cool.
> >>>>> >> >>>> >> >>>
> >>>>> >> >>>> >> >>> How long did the convert take? I'm
> wondering how
> >>>>> fast is hg
> >>>>> >> >>>> >> >>> convert.
> >>>>> >> >>>> >> >>>
> >>>>> >> >>>> >> >>> cheers,
> >>>>> >> >>>> >> >>> Szczepan
> >>>>> >> >>>> >> >>>
> >>>>> >> >>>> >> >>> On Mon, Sep 29, 2008 at 9:59 AM, Dan North
> >>>>> >> >>>> >> >>> <tast...@gmail.com
> <mailto:tast...@gmail.com> <mailto:tast...@gmail.com
> >>>>> <mailto:robert...@gmail.com
> <mailto:robert...@gmail.com>>>
> >>>>> >> >>>> >> >>> >>
> >>>>> >> >>>> >> >>> >> On 25 Sep, 15:35, szczepiq
> <szcze...@gmail.com <mailto:szcze...@gmail.com>
> >>>>> <mailto:szcze...@gmail.com <mailto:szcze...@gmail.com>>>
Well, as of mockito 1.5, if a method returns a primitive, it is a value.If it returns an object, it returns null, which is not a value.Rather than null, I would like it to return a "null object", if you are familiar with that pattern.Such an object makes it possible to continue executing a lot longer.In this context, the implementation of a "null object" is a mock of that object's class.An alternative approach would be to throw an exception, say "NotStubbedException".But you could use "verify" if you wish to control that no methods are called that you didn't expect.Oh, and that is one of the charming things about Mockito, you don't have to set up expectation for everything your unit under test is doing.I now like to know if you still potentially disagree with me. :)/Per
Per Lundholm wrote:
> Might have been the wrong sender - my bad.
>
> On Sun, Nov 2, 2008 at 8:23 AM, Per Lundholm <per.lu...@crisp.se
> <mailto:per.lu...@crisp.se>> wrote:
>
> Well, as of mockito 1.5, if a method returns a primitive, it is a
> value.
>
> If it returns an object, it returns null, which is not a value.
>
> Rather than null, I would like it to return a "null object", if you
> are familiar with that pattern.
>
> Such an object makes it possible to continue executing a lot longer.
>
> In this context, the implementation of a "null object" is a mock of
> that object's class.
>
> An alternative approach would be to throw an exception, say
> "NotStubbedException".
>
> But you could use "verify" if you wish to control that no methods
> are called that you didn't expect.
Correct me if I'm wrong, but I don't believe there is a way to tell
Mockito to verify() that no unstubbed method was invoked. That is, I
could verify that *specific* methods weren't invoked, but there is no
way to verify that none of the unstubbed methods were invoked.
> Oh, and that is one of the charming things about Mockito, you don't
> have to set up expectation for everything your unit under test is
> doing.
>
> I now like to know if you still potentially disagree with me. :)
I'd have to try this out for myself before I making up my mind. In
theory this could work very nicely, but I want to try it out to fully
understand the implications.
For example, what happens if I don't stub EntityManager and my code
invokes:
Query query = EntityManager.createQuery("queryString");
List result = query.getResultList();
return result;
then later on some code invokes:
if (result.contains(referenceObject))
...
This is pretty critical logic and it's not clear what Mockito should do
in this case (I would expect it to throw an exception because who knows
whether returning true or false is expected here). In this particular
case, I'd want to stub the Query to return a database row and if I
forget to do so that's a problem. At least that's what makes sense to me
now...
Gili
You would have to explicitly mention them one by one. From the examples:
verify(mockOne, never()).add("two");
Or you could say that a mock is never involved in any interactions
//verify that other mocks were not interacted
verifyZeroInteractions(mockTwo, mockThree);
Now you example:
> For example, what happens if I don't stub EntityManager and my code
> invokes:
>
> Query query = EntityManager.createQuery("queryString");
> List result = query.getResultList();
> return result;
>
> then later on some code invokes:
>
> if (result.contains(referenceObject))
> ...
>
> This is pretty critical logic and it's not clear what Mockito should do
> in this case (I would expect it to throw an exception because who knows
> whether returning true or false is expected here). In this particular
> case, I'd want to stub the Query to return a database row and if I
> forget to do so that's a problem. At least that's what makes sense to me
> now...
Well, to me, it seems that EntityManager is a mock and you have not
stubbed createQuery, right?
Then createQuery will return a mock of Query and its getResultList
will return a mock of a List.
The method "contains" returns a boolean for which the default value is "false".
If you would want something else to happen, you should stub so. ;-)
I may have made some stupid mistakes above, so someone (szczepiq,
North) should better read this
and slap me.
Cheers!
Per
Gili