Photo by Alexandru-Bogdan Ghita on Unsplash

A Simple, Modern, Mockito Project

Follow Along from Project Creation to Test Class

Kenneth Kousen
6 min readApr 5, 2023

--

https://pragprog.com/newsletter/

Let’s create a project that uses Mockito, showing the basic steps and how to use the API in a simple but (at least somewhat) realistic situation.

First, we need a Java project. I’ll use Java 17 here, but that’s not required. To use the latest version of Mockito (5.1.1 at time of writing), the minimum Java requirement is version 11. If you are still on Java 8, use a Mockito version in the 4s, which still work just fine.

📝 Mockito version 5 builds the inline mockmaker into the core, which makes it easy to test methods that are static or final , but that’s not needed here and will be the subject of a later post in this series.

Project Dependencies

Assuming you created a standard project structure with directories src/main/java and src/test/java, here is a Gradle build file for the root with the needed dependencies:

plugins {
id 'java'
}

group 'com.kousenit'
version '1.0'

repositories {
mavenCentral()
}

dependencies {
// JUnit 5
testImplementation…

--

--

Kenneth Kousen
The Pragmatic Programmers

Author of the books Mockito Made Clear, Help Your Boss Help You, Kotlin Cookbook, Modern Java Recipes, Gradle Recipes for Android, and Making Java Groovy