Introduction

Welcome to the final lesson of the "Increasing Code Test Coverage" course! Throughout our journey, we've examined the significance of code coverage, the function of characterization tests, and how interfaces and mocks can improve testability. Now, we'll integrate these concepts to achieve thorough code coverage by combining test writing and mocking techniques. This lesson will help us solidify our understanding and prepare us for practical application.

Whole Problem: The UserProcessor Class

Let's examine an implementation of a user processor. We want to add tests to describe the characteristics of the system. We also don't want our tests to depend on a database. Let's take a closer look at the implementation of the UserProcessor class:

The class uses a UserDatabase class as a dependency. This class can be viewed below:

Practice approach

First, we will focus on adding an interface for the database layer to enable us to mock this particular dependency. After that, we will focus on writing a collection of tests that will describe the system and help us increase code test coverage for the UserProcessor class.

Best Practices for Test Writing

When writing tests with mocks, consider the following best practices:

  • Focus on Behavior: Ensure tests verify the behavior of the code, not the implementation details.
  • Use Descriptive Names: Name tests clearly to indicate the scenario being tested.
  • Verify Interactions: Use mock verifications to ensure the correct methods are called.
  • Handle Exceptions: Test how the code handles exceptions to ensure robustness.

By following these practices, we can create reliable and maintainable tests that provide confidence in our code.

Practice Preparation

As we move on to the practice exercises, we'll have the opportunity to apply all the things we have learned so far in this course. Good luck, and enjoy the journey of mastering code test coverage!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal