Java Unit Testing


Java Unit Testing

Authors: Jim Weaver, Nicolae Caprarescu, Catalin Tudose

Unit testing has become standard practice for today's software developers. The goal of this learning path is to help you understand how to write clean, testable code, all the way... Read more

What you will learn

-- Creating unit tests with JUnit -- Mocking dependencies with Mockito -- Validating method calls with Mockito -- Creating partial mock classes -- Test-driven development -- Creating unit tests for legacy codebases


Familiarity with the Java programming language


Writing unit tests requires a solid foundation in the tools of the trade, and that is what will get with the courses in this section. You will learn about JUnit, the most popular unit testing framework for Java and Mockito, the most widely used mocking framework.

Getting Started Unit Testing with JUnit 5

by Jim Weaver

Nov 8, 2018 / 1h 29m

1h 29m

Start Course

Unit test frameworks like JUnit 5 help guide the design of your code as well as ensure that it works. In this course, Getting Started Unit Testing with JUnit 5, you'll get up to speed on the key skill of unit testing by leveraging the latest version of the popular JUnit unit test framework, JUnit 5. First, you'll learn how to validate your code with assertions and leverage JUnit 5 lifecycle methods. Next, you'll explore how to categorize tests, test exception handling, produce clear test reports, and many other JUnit 5 capabilities. Finally, you'll delve into two techniques critical to learn for unit testing - how to handle pre-existing untested code and how to drive development from a unit test. By the end of this course, you'll have the necessary knowledge to efficiently begin unit testing your own projects with JUnit 5.

Table of contents
  1. Course Overview
  2. Writing Your First Test
  3. Writing More Complex Tests
  4. Making Existing Code Testable
  5. Writing Tests First
  6. Expanding Your Knowledge

Getting Started with Mockito

by Nicolae Caprarescu

Aug 2, 2019 / 1h 24m

1h 24m

Start Course

Writing unit tests is difficult if the dependencies between your application classes make it difficult to draw a boundary between what you want to test and the rest of the codebase. In this course, Getting Started with Mockito, you will gain the ability to focus on the thing you’re trying to test without having to worry about the rest of the codebase. First, you will learn how to use mock objects instead of real dependencies. Next, you will discover how to configure mocked methods, including return values and exception throwing. Finally, you will explore the concept of partial mocks and how to use them to test tricky situations. When you are finished with this course, you will have the skills and knowledge of mocking with Mockito needed to test your Java applications by creating, configuring, and using mocks in your unit tests.

Table of contents
  1. Course Overview
  2. Introduction to Mockito
  3. Configuring Return Values of Mocked Methods
  4. Verifying That Mocked Methods Behave Accordingly
  5. Advanced Mocking Techniques
  6. Behavior-driven Development (BDD)


This section introduces you to the concept of Test-driven development, where you write tests first and then the code that makes those tests pass. Whether you choose to use TDD all of the time or only part of the time, this is an important technique to add to your developer toolkit.

TDD with JUnit 5

by Catalin Tudose

Jul 23, 2018 / 1h 17m

1h 17m

Start Course

At the core of every project, there is always extensive testing. In this course, TDD with JUnit 5, you’ll learn the benefits of TDD (Test-driven Development) and help you create TDD applications using the JUnit 5 testing framework. First, you'll learn how to develop TDD applications and how to use mock objects with JUnit 5. Next, you'll discover how to move a non-TDD application into the platform. Finally, you'll dive into how to integrate JUnit5 into your workflow. By the end of this course, you'll have the know how to develop safe and flexible Java applications using TDD with JUnit 5.

Table of contents
  1. Course Overview
  2. Introducing TDD and JUnit5
  3. Adding New Features to the Application Using TDD and JUnit 5
  4. TDD by Integrating JUnit 5 with Mockito


Writing unit tests for existing code bases can present unique challenges but in this section, you will learn to overcome those so that you can create tests and have the same confidence in your legacy applications as you do with newly developed code.

Unit Testing Legacy Code in Java

by Jim Weaver

May 21, 2018 / 2h 8m

2h 8m

Start Course

One of the biggest challenges developers encounter in their career is having to maintain an existing codebase with little or no tests and unclear, tangled code. In this course, Unit Testing Legacy Code in Java, you will learn techniques to get a legacy and untested Java application, a small but realistic Java application to provide services to a hospital emergency room, under test incrementally as you work on it. First, you will learn about different types of tests and test doubles that can be applied to create a safety net for the application gradually. Next, you will discover crucial dependency-breaking techniques that will allow you to create seams to provide test doubles without disrupting the code. Finally, you will explore ways to deal with hard to understand code, improving the clarity as you work, and techniques to leverage new, tested code from hard to understand code. When you are finished with this course, you will have the knowledge to efficiently handle untested systems you’ve become responsible for.

Table of contents
  1. Course Overview
  2. Introducing Our Legacy System
  3. Understanding the Role of Different Types of Tests
  4. Testing Code with Problematic Dependencies
  5. Testing Hard to Understand Code
  6. Expanding Your Skills