421

I want to verify if a method is called at least once through mockito verify. I used verify and it complains like this:

org.mockito.exceptions.verification.TooManyActualInvocations: 
Wanted 1 time:
But was 2 times. Undesired invocation:
qba47
  • 7
  • 1
  • 4
Ahmad Beg
  • 4,285
  • 3
  • 14
  • 5

2 Answers2

702

Using the appropriate VerificationMode:

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

verify(mockObject, atLeast(2)).someMethod("was called at least twice");
verify(mockObject, times(3)).someMethod("was called exactly three times");
Community
  • 1
  • 1
Liosan
  • 7,520
  • 2
  • 17
  • 16
  • 25
    You can also use `Mockito.times(...)` instead of `VerificationModeFactory.times(...)` for the static import – Wim Deblauwe Nov 24 '15 at 10:16
  • 28
    `import static org.mockito.Mockito.times;`. Generally importing packages with "internal" in them (`import static org.mockito.internal.verification.VerificationModeFactory.times;`) is discouraged. – Roger May 31 '16 at 20:52
  • is there another way of writing times(1) ? – Glenn Bech Aug 10 '17 at 10:57
  • 5
    @GlennBech [You can just omit that](http://static.javadoc.io/org.mockito/mockito-core/2.8.47/org/mockito/Mockito.html#4), it's implicit; the default `verify(mockObject).someMethod("")` looks for exactly 1 interaction (no more, no less). If, instead, you want _at least one_ invocation of the method, you can use the `atLeastOnce()` specifier. – nbrooks Aug 10 '17 at 21:23
  • 1
    @Roger I agree with you, for a beginning TDD practicer like me, static import make me more confuse about remembering the methods or which framework is using (Mockito, Espresso, or just normal unit test). – Phong Nguyen Oct 04 '17 at 01:58
8

For Kotlin:

build gradle:

testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"

code:

interface MyCallback {
  fun someMethod(value: String)
}

class MyTestableManager(private val callback: MyCallback){
  fun perform(){
    callback.someMethod("first")
    callback.someMethod("second")
    callback.someMethod("third")
  }
}

test:

import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.mock
...
val callback: MyCallback = mock()
val uut = MyTestableManager(callback)
uut.perform()

val captor: KArgumentCaptor<String> = com.nhaarman.mockitokotlin2.argumentCaptor<String>()

verify(callback, times(3)).someMethod(captor.capture())

assertTrue(captor.allValues[0] == "first")
assertTrue(captor.allValues[1] == "second")
assertTrue(captor.allValues[2] == "third")

For Java:

Lombok used to simplify. You can also type out the constructor if you prefer.

build gradle:

testImplementation "org.mockito:mockito-core:3.6.28"

code:

// MyCallback.java
public interface MyCallback {
  void someMethod(String value);
}
// MyTestableManager.java
public class MyTestableManager {
  private MyCallback callback;

  public MyTestableManager(MyCallback callback) {
    this.callback = callback;
  }

  public void perform(){
    callback.someMethod("first");
    callback.someMethod("second");
    callback.someMethod("third");
  }
}

test:

import org.mockito.Mockito.times;
import org.mockito.Mockito.verify;
import org.mockito.Mock;
import org.mockito.Captor;
// whatever other imports you need
@Mock
private MyCallback callback;
@Captor
private ArgumentCaptor<String> captor;

private MyTestableManager uut = new MyTestableManager(callback);

// in your test method:
uut.perform()

verify(callback, times(3)).someMethod(captor.capture())

assertTrue(captor.getAllValues().get(0) == "first")
assertTrue(captor.getAllValues().get(1) == "second")
assertTrue(captor.getAllValues().get(2) == "third")
Egor Hans
  • 206
  • 1
  • 11
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • In case you wonder about my edits: Annotation-based mock creation is usually preferable in Java, but I wasn't sure if it's a thing in Mockito Kotlin. As for renaming manager to uut, that's just conventions - the object that's being tested (the unit under test) is usually named uut or sut (not sure what the latter stands for). – Egor Hans Apr 06 '21 at 06:47
  • Thank you for elaborating about the `captor` mecanism, works great! – Jonathan F. Aug 11 '23 at 12:50