Mockito.atLeast(0), want 0 to many times

3,412 views
Skip to first unread message

lukewpatterson

unread,
Aug 13, 2009, 1:53:53 PM8/13/09
to mockito
So I'm trying to verify that a method is called 0 to many times. Here
is what I've tried so far:

---------------------------------------------------------------------
interface Laugher {

void laugh();

}

class StubLaugherImplementation implements Laugher {

void laugh(){
System.out.println("haha");
}

}

Laugher laugher = Mockito.spy(new StubLaugherImplementation());

// do some stuff, pass laugher to someone

Mockito.verify(laugher, Mockito.atLeast(0)).laugh();

---------------------------------------------------------------------

The problem is, Mockito.atLeast(0) gives me:
org.mockito.exceptions.base.MockitoException: Negative value or zero
are not allowed here

Hopefully the solution is simple, I just can't seem to find it.

David Gageot

unread,
Aug 13, 2009, 2:07:44 PM8/13/09
to moc...@googlegroups.com
Hi,

Have you tried : Mockito.verify(laugher).laugh()  ?

David.
--
David Gageot

CTO - Tech4Quant
13-15 rue Taitbout
75009 Paris
06 13 13 89 54
01 73 02 89 91

David Gageot

unread,
Aug 13, 2009, 2:09:28 PM8/13/09
to moc...@googlegroups.com
Oups, I didn't read carefully. Forget what I said.

Why do you need to verify anything ? 0 to many times mean
you don't care if it's called. do you ?

David.

Joe Kearney

unread,
Aug 13, 2009, 2:11:15 PM8/13/09
to moc...@googlegroups.com
>that a method is called 0 to many times
What exactly are you trying to verify? Zero inclusive? Surely that would be satisfied by every object.

2009/8/13 David Gageot <dga...@gmail.com>

lukewpatterson

unread,
Aug 13, 2009, 2:19:04 PM8/13/09
to mockito
> Why do you need to verify anything ? 0 to many times mean
> you don't care if it's called. do you ?
>

I should have explained my use case with a realistic example. I'm
stubbing some Maven API for a plugin I'm writing. I can't
realistically stub some of the stuff because the dependency graph
grows out of control (50 million methods with all sorts of complex
return types).

I want to verify that my plugin optionally calls only selected
methods, and nothing more. I was hoping I could list the acceptable/
included methods with "atLeast(0)"s and then
verifyNoMoreInteractions. It would be much easier to list the
"included" methods rather than the "excluded" methods.

Per Lundholm

unread,
Aug 13, 2009, 2:21:01 PM8/13/09
to moc...@googlegroups.com
Yeah, don't verify unless you really need to know. This is unlike JMock and EasyMock where you have a different philosophy, should that be what you're heading to.
/Per

Per Lundholm

unread,
Aug 13, 2009, 2:25:04 PM8/13/09
to moc...@googlegroups.com
Ok, so after a point in the execution, you'd like to say "there might have been some interactions, which is ok, but from now on, there should be no more interactions"?

1. do some stuff
2. point in execution where there might have been some interaction
3. do some more stuff
4. verify no more interaction

Correct?
/Per

Luke Patterson

unread,
Aug 13, 2009, 2:32:46 PM8/13/09
to moc...@googlegroups.com
On Thu, Aug 13, 2009 at 1:25 PM, Per Lundholm<per.lu...@gmail.com> wrote:
> Ok, so after a point in the execution, you'd like to say "there might have
> been some interactions, which is ok, but from now on, there should be no
> more interactions"?
>
> 1. do some stuff
> 2. point in execution where there might have been some interaction
> 3. do some more stuff
> 4. verify no more interaction
>
> Correct?

Here's the kind of class I need to stub:
http://maven.apache.org/ref/2.0.10/maven-project/apidocs/org/apache/maven/project/MavenProject.html

I want to know if my plugin tried to call some random methods on that
object, cause my stubs probably won't provide meaningful results for
those.

It's not that they plugin necessarily violates any contracts by
calling some random method, it's that I want to be confident my
stubbing was thorough enough.

Per Lundholm

unread,
Aug 13, 2009, 2:45:54 PM8/13/09
to moc...@googlegroups.com
I don't really know how to help you to do exactly what you want. Sorry.

But in my style of programming, I don't care about the stubbing unless I need to define a return value. The rest I leave to return some default and am happy with that.

I seldom use "verify". An e.g. would be some GUI logic where I like to verify that a service is called.

/Per

lukewpatterson

unread,
Aug 13, 2009, 5:55:29 PM8/13/09
to mockito
If anyone else is interested in this functionality, you can follow
http://code.google.com/p/mockito/issues/detail?id=111
Reply all
Reply to author
Forward
0 new messages