Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

This cookbook illustrates how to use Mockito verify in a variety of use cases.

The format of the cookbook is example-focused and practical — no extraneous details and explanations are necessary.

We’re going to be mocking a simple list implementation:

public class MyList extends AbstractList<String> {

    @Override
    public String get(final int index) {
        return null;
    }
    
    @Override
    public int size() {
        return 1;
    }
}

Further reading:

Mocking Exception Throwing using Mockito

Learn to configure a method call to throw an exception in Mockito.

Mockito Support for Optional, Streams, Lambda Expressions

Overview of Java 8 support in Mockito framework, including Streams and default interface methods

Mocking of Private Methods Using PowerMock

Learn how PowerMock can be used to extend the capability of Mockito for mocking and verification of private methods in the class under test.

2. The Cookbook

Verify simple invocation on mock:

List<String> mockedList = mock(MyList.class);
mockedList.size();

verify(mockedList).size();

Verify the number of interactions with mock:

List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, times(1)).size();

Verify no interaction with the whole mock occurred:

List<String> mockedList = mock(MyList.class);
verifyNoInteractions(mockedList);

Verify no interaction with a specific method occurred:

List<String> mockedList = mock(MyList.class);
verify(mockedList, times(0)).size();

Verify there are no unexpected interactions — this should fail:

List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.clear();

verify(mockedList).size();
assertThrows(NoInteractionsWanted.class, () -> verifyNoMoreInteractions(mockedList));

Verify the order of interactions:

List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.add("a parameter");
mockedList.clear();

InOrder inOrder = Mockito.inOrder(mockedList);
inOrder.verify(mockedList).size();
inOrder.verify(mockedList).add("a parameter");
inOrder.verify(mockedList).clear();

Verify an interaction has not occurred:

List<String> mockedList = mock(MyList.class);
mockedList.size();

verify(mockedList, never()).clear();

Verify an interaction has occurred at least a certain number of times:

List<String> mockedList = mock(MyList.class);
mockedList.clear();
mockedList.clear();
mockedList.clear();

verify(mockedList, atLeast(1)).clear();
verify(mockedList, atMost(10)).clear();

Verify interaction with the exact argument:

List<String> mockedList = mock(MyList.class);
mockedList.add("test");

verify(mockedList).add("test");

Verify interaction with flexible/any argument:

List<String> mockedList = mock(MyList.class);
mockedList.add("test");

verify(mockedList).add(anyString());

Verify interaction using argument capture:

List<String> mockedList = mock(MyList.class);
mockedList.addAll(Lists.<String> newArrayList("someElement"));

ArgumentCaptor<List<String>> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(mockedList).addAll(argumentCaptor.capture());

List<String> capturedArgument = argumentCaptor.getValue();
assertThat(capturedArgument).contains("someElement");

3. Conclusion

The goal of this guide is to have this information readily available online. I’ve published a few similar development cookbooks on Google Guava and Hamcrest.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW