Mockito Tutorials

Mockito Tutorials

In this detailed Resource page, we feature an abundance of Mockito Tutorials!

Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD).

The framework’s name and logo are a play on mojitos, a type of drink.

Mockito allows developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand. One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test. Mockito attempts to eliminate the expect-run-verify pattern by removing the specification of expectations. Mockito also provides some annotations for reducing boilerplate code.

Note
If you wish to build up your Mockito knowledge first, check out our Mockito Tutorial for Beginners.

Mockito Tutorials – Introduction

Mockito Tutorial: Testing and Mocking with Mockito

  • Getting started with Mockito
    In this lesson, you will get started with Mockito. You will learn why and when Mocks should be used, and how to perform the two phases of Mockito, Stubbing and Verification. Then, you will see how to integrate Mockito to your projects and some of its best practices.
  • Mocks, Spies, Partial Mocks and Stubbing
    Now we are now ready to get into more details regarding Mocks, Stubs and Spies. We will take an in-depth look at stubbing classes and interfaces using Mockito, and we will also talk about Stubbing void methods and Stubbing return values. Finally, we will discuss Argument Matchers and Spies and Partial Stubbing.
  • Mockito Verification
    Verification is the process of confirming the behaviour of a Mock. It is useful in determining that the class we are testing has interacted in an expected way with any of its dependencies. We will look in detail at the verification phase of Mockito and examine the ways which we can verify behaviour out of the box.
  • Hamcrest matchers tutorial
    Hamcrest is a framework for creating matcher objects. These matcher objects are predicates and are used to write rules which can be satisfied under certain conditions. In this tutorial we will look at the Hamcrest Matcher library and how to integrate it with JUnit and Mockito.
  • Custom Hamcrest matchers
    From time to time we will run up against the limits of the Hamcrest library of Matchers. In this tutorial we will create matchers for various circumstances, using the tools which Hamcrest provides. We will use the Hamcrest API to create our own Custom Matchers, in order to extend the ‘out of the box’ functionality that Hamcrest provides.

Mockito Tutorials – Functions

Learn the basic functionalities of Mockito

  • Mockito Hello World Example
    In this article you will learn how to use it for mock object creation, stubbing and verification. You will learn how to use it for mock object creation, stubbing and verification. I’ll also explain in detail how mock objects work, how they encourage testing based on behavior verification.
  • Mockito Mock Database Connection Example
    A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to write a simple test case using Mockito. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2
  • Mockito List Matcher Example
    This example will show you how to use Mockito matchers for lists, used when mocking methods that receive objects of classes allowing wildcards (among others). Apart from that, we will also show you what Mockito matchers don’t do or what are not thought for, since their use can lead to confusion.
  • Mockito Test Case Example
    A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to write a simple test case using Mockito.
  • Mockito: How to mock a void method call
    In this example we saw how we can mock void classes using Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to mock a void method call using Mockito.
  • Test-Driven Development With Mockito
    In this example we will learn how to do a Test Driven Development (TDD) using Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API.
  • Mockito Initmocks Example
    In this example we will learn how to initialize mocks in Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API.
  • Initializing Mockito with InitMocks
    In the Mockito Tutorial for Beginners, we saw a general overview of the use of Mockito framework for Java unit tests. In this example, we will see how we can use the InitMocks initialization method, for an easier but expressive way of creating the mocks, specially useful when we have several dependencies to mock.
  • Mockito Captor Example
    A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to use ArgumentCaptor class/ Captor annotation of Mockito.
  • Mockito ThenReturn Example
    In this example we will learn how to use ‘thenReturn’ method of Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2
  • Mockito Verify Example
    In this article, I am going to show you an example of Mockito Verify. To test the state, we use assert, likewise, to verify the test interactions, we use Mockito.verify.
  • Mockito void Method Example
    In this article we are going to show you how to stub a void method. In Mockito Hello World Example, we will learn how to stub a non-void method that returns something. Sometimes we may also need to stub a void method which is what I am going to show in this article.
  • Mockito Spy Example
    In this article, I am going to show you an example of Mockito Spy. There are times when we would like to use most of the original object’s behavior but mock only a portion of it. This is called spying objects, also called as partial mocking.

Mockito Tutorials – Integrations

Learn how to use Mockito in various platforms and frameworks

  • Powermock – Mockito Integration Example
    In this article, we will see an integration example of Powermock and Mockito but first let’s do the setup. Good thing about Powermock is that it doesn’t re-invent the testing framework and in fact enhances the testing frameworks like Easymock and Mockito.
  • Mockito mock static method example
    In this article, I am going to show you how to mock static methods using Powermockito. PowerMock is an open source mocking library. It extends the existing mocking frameworks, such as EasyMocks and Mockito, to add even more powerful features. One of them being mocking static methods.
  • Mockito Mock Private Method Example with PowerMock
    A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to mock a private method.
  • Mockito Maven Dependency Example
    In this example we will show you how to setup a maven dependency for Mockito using Eclipse. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to define Mockito dependency in maven and how to use it.
  • JUnit Mockito Example
    In this post, I’ll be showing one of the most widely used and popular JUnit Testing Mocking framework – Mockito. Methods and ways to create Unit Test cases have evolve ever since it’s introduction. New tools and API´s are now available and they provide a more advanced scheme on creating and executing JUnit Test cases.
  • Writing JUnit Test Cases Using Mockito
    In this example we will learn how to write JUnit tests using Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2
  • JUnit Mockito When thenReturn Example
    In this example we will show you the usage of JUnit Mockito When thenReturn method. JUnit has many capabilities for testing the unit test cases for Java. But sometimes it lacks some of the common functionalities. Where it lacks and how we can complete, we will be showing in this example.
  • Spring Test Mock Example
    A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to mock spring components using Mockito. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2

[undereg]

Back to top button