Mockito Tutorial: Testing and Mocking with Mockito

Course Overview

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).

As the official documentation states: “Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.”

In this course, you will dive into the magic of Mockito. You will learn about Mocks, Spies and Partial Mocks, and their corresponding Stubbing behavior. You will also see the process of Verification with Test Doubles and Object Matchers.

Finally, Test Driven Development (TDD) with Mockito is discussed in order to see how this library fits in the concept of TDD.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

About the Author

Hugh is a Senior Software Engineer and Certified Scrum Master based in Galway, Ireland. He achieved his B.Sc. in Applied Computing from Waterford Institute of Technology in 2002 and has been working in industry since then. He has worked for a several large blue chip software companies listed on both the NASDAQ and NYSE. His main areas of interest include Core Java, JEE, Spring, Design Patterns, TDD, Maven and Agile Software Development.


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, create our own Verification Modes and use Argument Captors for doing further more complex assertions on our Data.

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.

Make sure to retweet this, let your social followers know!

Exit mobile version