How can I ignore method calls during initialisation?

883 views
Skip to first unread message

Dieter Mai

unread,
Feb 8, 2014, 5:48:43 PM2/8/14
to moc...@googlegroups.com
Hallo 

I'm trying mockito and i like it.

but i have some problems. here is my first one

i am writing tests for a Class that needs to be initalized (but not always in the same whay). during this initialization methods of my mocks are called. this calles i would like to ignore in later verify(...) calles.

example:

public void test_myTestMethod()
{
    // initializastion
    testedClassInstance.init(); // <-- init will call myMock.foo()

    // testedMethode
    testedClassInstance.myMethod(); // <-- will call myMock.foo() too

    // verification
    verify( myMock).foo(); // this will fail because foo() was called twice
}


i would like to reset the call counter for myMock.foo() but i don't know how. do i need to call myMock.reset() in this case?


 

Eric Lefevre-Ardant

unread,
Feb 10, 2014, 3:26:01 AM2/10/14
to moc...@googlegroups.com
Well, the correct syntax would really be Mockito.reset(myMock), but yes, this is an option.

Personally, I would rather use something like this instead:
  verify(myMock, times(2)).foo();

Of course, alternatively, maybe this is an opportunity to look into refactoring your code. Often, technical issues with Mockito show that the production code was in need of refactoring.


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages