Tutorials  /  JavaScript

Mockito Verify - Tutorial

Ccentron Redaktion · February 2024 ·4 min read ·JavaScript, Tutorial

Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.

    • Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method.
    • We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified. If any method verification is still left, it will fail and provide proper message.
    • verifyZeroInteractions() behavior is same as verifyNoMoreInteractions() method.
    • We can use inOrder() method to verify the order of method invocation. We can skip a method invocation but the methods being verified must be in the same order.

Let’s look at some of the mockito verify method examples.

Mockito verify() simple example

Code
<Test>
void test() {
    List<String> mockList = mock(List.class);
    mockList.add("Pankaj");
    mockList.size();
    
    verify(mockList).add("Pankaj");
}

Above verify method will pass if add("Pankaj") is called only once on the mocked list object. It’s the same as calling with times(1) argument with verify method.

Code
verify(mockList, times(1)).size();

If we want to make sure a method is called but we don’t care about the argument, then we can use ArgumentMatchers with verify method.

Code
verify(mockList).add(anyString());
verify(mockList).add(any(String.class));
verify(mockList).add(ArgumentMatchers.any(String.class));

Note that org.mockito.Mockito class provides static methods for most of the useful methods in the Mockito framework, this helps us in writing fluent code by importing them using import static.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Mockito verify with number of times

Mockito verify() method is overloaded, the second one is verify(T mock, VerificationMode mode). We can use it to verify for the invocation count.

Code
verify(mockList, times(1)).size(); //same as normal verify method
verify(mockList, atLeastOnce()).size(); // must be called at least once
verify(mockList, atMost(2)).size(); // must be called at most 2 times
verify(mockList, atLeast(1)).size(); // must be called at least once
verify(mockList, never()).clear(); // must never be called

verifyNoMoreInteractions()

This method can be used after all the verify methods to make sure that all the interactions are verified. It will fail the test if there are any unverified interactions on the mocked object.

Code
// all interactions are verified, so below will pass
verifyNoMoreInteractions(mockList);
mockList.isEmpty();
// isEmpty() is not verified, so below will fail
verifyNoMoreInteractions(mockList);

verifyZeroInteractions()

verifyZeroInteractions() method behavior is same as verifyNoMoreInteractions() method.

Code
Map mockMap = mock(Map.class);
Set mockSet = mock(Set.class);
verify(mockList).isEmpty();
verifyZeroInteractions(mockList, mockMap, mockSet);

Mockito verify only method call

If we want to verify that only one method is being called, then we can use only() with verify method.

Code
Map mockMap = mock(Map.class);
mockMap.isEmpty();
verify(mockMap, only()).isEmpty();

Mockito Verify Order of Invocation

We can use InOrder to verify the order of invocation. We can skip any method to verify, but the methods being verified must be invoked in the same order.

Code
InOrder inOrder = inOrder(mockList, mockMap);
inOrder.verify(mockList).add("Pankaj");
inOrder.verify(mockList, calls(1)).size();
inOrder.verify(mockList).isEmpty();
inOrder.verify(mockMap).isEmpty();

Summary

Mockito verify() methods can be used to make sure the mock object methods are being called. If any method call is deleted by mistake, then verify method will throw an error.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren