JUnit and Mockito Unit Testing for Java Developers

Video description

Uncover the mechanics of JUnit, Mockito, and Hamcrest to transform the way you test your Java projects

About This Video

  • Find out why JUnit is the best framework for writing unit tests
  • Get hands-on exercises of JUnit and Mockito in action
  • Get to grips with modern unit testing concepts and best practices

In Detail

Are you looking for a simple, no-nonsense guide to learning how unit testing works? This video course is what you need. Complete with hands-on exercises and best practices, this guide will help you to understand JUnit and Mockito unit testing with ease.

The course starts with an introduction to unit testing and highlights the need and importance of its testing framework, JUnit. You’ll then understand the project structure and conventions by creating a Java project using JUnit. As you implement best practices for writing and running unit tests with JUnit and IntelliJ IDEA, you’ll discover how JUnit works. You’ll also write mock objects-based unit tests with Mockito for testing a simple layered architecture. Finally, you’ll learn to make more powerful assertions in your unit tests by harnessing the power of the Hamcrest assertions library that ships with JUnit.

By the end of this course, you’ll have developed the skills needed for writing better unit tests using JUnit, Mockito, and Hamcrest.


Whether you are a Java developer or a tester who wants to write unit tests using JUnit, Mockito, and Hamcrest, this course is for you. A basic understanding of Java is necessary to get started with this course.

Publisher resources

Download Example Code

Table of contents

  1. Chapter 1 : Introduction
    1. Welcome to the Course
    2. Overview of the Course Structure
  2. Chapter 2 : Unit Testing Foundations
    1. Section Introduction
    2. What is Unit Testing?
    3. What is JUnit?
    4. Introducing a Simple Unit Test in JUnit
    5. Structure of a Unit Test (Arrange-Act-Assert and Given/When/Then)
    6. Conventions for Writing Unit Tests
    7. Characteristics of Proper Unit Tests
    8. What are Assertions?
  3. Chapter 3 : Creating a Java Project Using JUnit
    1. Section Introduction
    2. Creating a Java Project Using the Maven Quickstart Archetype in JUnit
    3. Introducing a Typical Java Project Structure (Production vs Test Source Trees)
    4. Understanding the Basic Conventions for Creating Unit Tests (Package and Naming)
  4. Chapter 4 : Writing and Running Unit Tests with JUnit and IntelliJ IDEA
    1. Section Introduction
    2. Creating a Test Method with @Test Annotation
    3. Running a Unit Test Within the Integrated Development Environment (IDE) and Understanding Test Success
    4. Understanding Test Failures with Exceptions
    5. Failing a Unit Test Explicitly with Assert.Fail()
    6. Using Assert.Fail() to Check Validation Logic in a Try/Catch
    7. Introduction to the JUnit Assertions API
    8. Structuring Unit Tests with Arrange-Act Assert
    9. Understanding the Execution of Assertions and JUnit's Fail-fast Test Failure Mechanism
    10. Adding Descriptive Comments to Assertions and Test Failures to Aid Diagnostics
    11. Coding a Unit Test Following Arrange-Act-Assert
    12. Keeping Test Setup (Don't Repeat Yourself) DRY with @Before
    13. Understanding Test Fixture Instantiation
  5. Chapter 5 : Using Testing with Mock Objects and Mockito
    1. Section Introduction
    2. What Is Mockito and What Are Mock Objects?
    3. Introduction to Testing with Mock Objects
    4. Configuring Projects Using Mockito
    5. Creating a Project Which Uses JUnit and Mockito
    6. Coding a Layered Architecture - Presentation Layer
    7. Coding a Layered Architecture - Business Layer
    8. Coding a Layered Architecture - Persistence Layer
    9. Putting the Correct Packages in Place to Respect the layering
    10. Writing a Test for the Login Controller in the Presentation Layer
    11. Understanding the Test Fixture Setup While Writing Mock Object-based Unit Tests
    12. Writing a Test for the Authentication Service in the Business Layer
    13. Thinking about Design Decisions When Unit Testing
  6. Chapter 6 : Looking at Hamcrest Assertions Library
    1. Section Introduction
    2. Evolving the Domain and Repository Classes
    3. Ignoring Tests with @ignore
    4. Implementing a Finder in the Repository
    5. Implementing Business Logic in the Service
    6. Configuring Maven to Use Java 8
    7. Evolving the Repository Class
    8. Adding a Parameter-driven Finder to the Service
    9. Refactoring to Evolve a New Service
    10. Generating Test Methods in the IDE and Toggling Between Production and Test Code
    11. Introducing the JUnit Mockito Runner and @Mock
    12. Analyzing the Production Code to Determine How to Write the Arrange Section
    13. Using Mockito's @InjectMocks to Create the System Under Test with its Dependencies
    14. Keep Yourself Motivated by Checking the Happy Path Test Output
    15. Using the JUnit Assertions API to Write the Assert Section
    16. Thinking about the Weaknesses of the JUnit Assertions API
    17. Importance of Checking Your Work as You Go!
    18. Upgrading a JUnit Project to Use the Full Hamcrest Version
    19. Replacing JUnit Assertions with Hamcrest Assertions and Using Assertions with Collections
    20. Understanding and Reading Hamcrest Test Failures
    21. Quick Thoughts on Hamcrest Individual vs Collection Assertions
    22. Do Not Invoke Unneeded Production Code in Your Test Classes
    23. Understanding Evaluation of Hamcrest Matchers
    24. Thinking about Keeping Production Code to a Minimum in Tests
    25. Creating Complex Hamcrest Assertions with allOf Method and Property Matchers

Product information

  • Title: JUnit and Mockito Unit Testing for Java Developers
  • Author(s): Matthew Speake
  • Release date: October 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781801078337