15 Testing your Spring app

 

This chapter covers

  • Why testing apps is important
  • How tests work
  • Implementing unit tests for Spring apps
  • Implementing Spring integration tests

In this chapter, you’ll learn to implement tests for your Spring apps. A test is a small piece of logic whose purpose is to validate that a specific capability your app implements works as expected. We’ll classify the tests into two categories:

  • Unit tests—Focus only on an isolated piece of logic
  • Integration tests—Focus on validating that multiple components correctly interact with each other

But when I only use the term “test,” I refer to both these categories.

Tests are essential for any application. They ensure that the changes we make during the app’s development process don’t break existing capabilities (or at least they make errors less likely) and also serve as documentation. Many developers (unfortunately) disregard tests because they are not directly part of the app’s business logic, and, of course, it takes some time to write them. Because of this, tests seem to not have a significant impact. Indeed, their impact is not usually visible in the short term, but trust me, tests are invaluable in the long term. I can’t stress enough how important it is to make sure you properly test your app’s logic.

Why should you write a test instead of relying on manually testing a capability?

15.1 Writing correctly implemented tests

15.2 Implementing tests in Spring apps

15.2.1 Implementing unit tests

15.2.2 Implementing integration tests

Summary