Configs2026年4月27日·1 分钟阅读

Mockito — The Most Popular Java Mocking Framework

Mockito lets Java developers create clean mock objects and verify interactions with a simple, readable API.

Introduction

Mockito is the most used mocking framework in the Java ecosystem. It allows developers to create mock objects, stub method returns, and verify interactions without writing boilerplate. Mockito integrates seamlessly with JUnit and TestNG, and its clean API has influenced mocking libraries in other languages.

What Mockito Does

  • Creates mock instances of classes and interfaces with a single mock() call
  • Stubs return values, exceptions, and callbacks using when(...).thenReturn(...)
  • Verifies that methods were called with expected arguments and call counts
  • Supports argument matchers like any(), eq(), and custom matchers
  • Provides @Mock and @InjectMocks annotations for declarative setup

Architecture Overview

Mockito uses ByteBuddy to generate subclass proxies of the target type at runtime. When a stubbed method is invoked, the proxy intercepts the call and returns the configured value from an internal invocation container. Verification walks the recorded invocation log and matches against expected argument patterns. Inline mock making (the default since Mockito 5) instruments the class via a Java agent, allowing mocking of final classes and methods without extra configuration.

Self-Hosting & Configuration

  • Add mockito-core via Maven or Gradle as a test-scoped dependency
  • Use mockito-junit-jupiter for JUnit 5 extension support with @ExtendWith
  • Enable inline mock making (default in Mockito 5) for final class mocking
  • Configure strict stubbing via Mockito.lenient() or MockitoSettings to control unused stub warnings
  • Pair with mockito-inline on older versions for final class support

Key Features

  • Clean, readable API that reads like natural language: when, then, verify
  • Argument captors let you capture and inspect values passed to mocked methods
  • Spy objects wrap real instances so you can stub specific methods while keeping others real
  • BDD-style aliases (given, then) for teams preferring behavior-driven conventions
  • Deep stubs allow chaining mock returns across nested method calls

Comparison with Similar Tools

  • EasyMock — requires explicit replay/verify lifecycle; Mockito has a simpler arrange-act-assert flow
  • PowerMock — can mock statics and constructors; Mockito 5 handles final classes natively
  • JMockit — uses instrumentation for broader mocking; Mockito has a larger community and ecosystem
  • WireMock — mocks HTTP APIs at the network level; Mockito mocks Java objects in-process
  • Spock — Groovy-based framework with built-in mocking; Mockito is Java-native and IDE-friendly

FAQ

Q: Can Mockito mock static methods? A: Yes, since version 3.4. Use try (var mocked = mockStatic(MyClass.class)) to scope the static mock.

Q: Does Mockito work with Kotlin? A: Yes. The mockito-kotlin library adds Kotlin-friendly extensions and reified type support.

Q: How do I mock a void method? A: Use doNothing().when(mock).voidMethod() or doThrow() for exception testing.

Q: Is Mockito thread-safe? A: Mock objects are thread-safe for stubbing and verification, but concurrent stubbing of the same mock should be avoided.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产