15 Unit testing in Spring apps
This chapter covers
- Why testing apps is important
- How tests work
- Implementing unit tests for 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. On the one hand, we have unit tests. Unit tests focus only on an isolated piece of logic. On the other hand, there are integration tests which focus on validating that multiple components correctly interact with each other. We’ll refer strictly to unit tests for now, 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 they 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, which makes tests seem not to have a significant impact. Indeed, their effect is not usually visible in the short term, but tests are invaluable in the long term.