Mockito

用Java编写、用于单元测试的、最受欢迎的模拟框架。(Most popular Mocking framework for unit tests written in Java. )

Github stars Tracking Chart

Mockito是一个模拟框架,味道非常好。它允许您使用干净简单的API编写漂亮的测试。 Mockito不给你宿醉,因为测试非常可读,并且会产生干净的验证错误。

大规模的StackOverflow社区投票Mockito是Java的最佳模拟框架。所有库中的十大Java库,不仅是测试工具。在2013年底,对30,000个GitHub项目进行了分析。虽然Mockito在主要报告中达到9号,但是mockito-core和mockito都是同一个工具,因此Mockito的事实位置是第4位,超越着名的工具,如Guava或Spring。把这个研究看作是一个巨大影响的指标,这个模型每天都在用Java编写的单元测试中进行。

Java模拟由诸如EasyMock或jMock之类的预期运行验证库控制。 Mockito提供更简单和更直观的方法:您在执行后提出有关交互的问题。使用mockito,你可以验证你想要什么。使用预期运行验证库,您经常被迫查看不相关的交互。

没有任何预期的验证,也意味着Mockito的模拟通常在没有昂贵的设置的情况下就已经准备好了。他们的目标是保持透明,让开发人员专注于测试所选的行为,而不是吸引注意力。

Mockito的API非常小,启动模拟几乎不花时间。只有一种模拟,只有一种创建模拟的方法。只要记住,执行之前的存根,之后的交互验证。您很快就会注意到,在TDD编写java代码时,这种模拟是多么的自然。

Mockito具有与EasyMock相似的语法,因此您可以安全地重构。 Mockito不明白“期望”的概念。这里只有存根和验证。

Mockito实现了Gerard Meszaros所说的测试间谍。

Overview

Name With Ownermockito/mockito
Primary LanguageJava
Program languageJava (Language Count: 5)
Platform
License:MIT License
Release Count635
Last Release Namev5.11.0 (Posted on )
First Release Namev1.7-rc1 (Posted on )
Created At2012-10-13 20:27:12
Pushed At2024-04-27 15:19:22
Last Commit At
Stargazers Count14.6k
Watchers Count425
Fork Count2.5k
Commits Count6.1k
Has Issues Enabled
Issues Count1629
Issue Open Count385
Pull Requests Count1348
Pull Requests Open Count19
Pull Requests Close Count319
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Most popular mocking framework for Java

Build Status Coverage Status MIT License

latest release
Bintray
Maven Central
Javadocs

Current version is 2.x

Still on Mockito 1.x? See what's new in Mockito 2!

Mockito continuously delivers improvements using Shipkit library (http://shipkit.org). See the latest release notes and latest documentation. Docs in javadoc.io are available 24h after release. Read also about semantic versioning in Mockito.

Older 1.x releases are available in
[Central Repository](http://search.maven.org/#artifactdetails, org.mockito, mockito-core, 1.10.19, jar)
, Bintray
and javadoc.io (documentation).

More information

All you want to know about Mockito is hosted at The Mockito Site which is Open Source and likes pull requests, too.

Want to contribute? Take a look at the Contributing Guide.

Enjoy Mockito!

Need help?

How to develop Mockito?

To build locally:

 ./gradlew build

To develop in IntelliJ IDEA you can use built-in Gradle import wizard in IDEA.
Alternatively generate the importable IDEA metadata files using:

 ./gradlew idea

Then, open the generated *.ipr file in IDEA.

How to release new version?

Mockito implements Continuous Delivery model.
Every change on main branch (for example merging a pull request) triggers a release build on Travis CI.
The build publishes new version if specific criteria are met: all tests green, no 'ci skip release' used in commit message, see the build log for more information.
Every new version is published to "mockito/maven" Bintray repository.
New versions that Mockito team deems "notable" are additionally published to Maven Central and JCenter.
We used to publish every version to Maven Central but we changed this strategy based on feedback from the community (#911).

  • Q: What's new in Mockito release model?

    A: In Q2 2017 we implemented Mockito Continuous Delivery Pipeline 2.0.
    Not every version is published to Maven Central.

  • Q: How to publish to Maven Central?

    A: Include "[ci maven-central-release]" in the merge commit when merging the PR.
    Hint: To signify a new feature consider updating version to next minor/major, like: "2.8.0", "2.9.0", "3.0.0".

  • Q: How to promote already released version to a notable version?

    A: It isn't automated at the moment. What's the use case?

To the top