Conteúdo do Curso
Spring Boot Backend
Spring Boot Backend
Fundamentals of Testing Backend Applications
Testing helps us identify potential errors and ensures the system's stability at all stages of development. An application can only be deemed fully ready for use after tests have been written.
What is Backend Application Testing?
This is a part of development that many backend developers dislike, as it requires writing tests for almost all created code. This means checking every step of a specific method's operation to ensure its accuracy.
Note
However, testing can save your application or company from hidden errors that may have emerged when adding new features and gone unnoticed.
Example from Real Life When Tests are Beneficial
Let's consider an online store. When updating the code for the shopping cart, a developer might accidentally affect the discount calculation logic. Without tests, this error might only surface in production when customers notice incorrect pricing.
However, if tests for price and discount calculations are written, the error will be caught during the development phase, preventing issues in production and customer dissatisfaction.
Note
If the developer had not written tests and users were presented with incorrect prices, the business would likely have lost a significant amount of money.
What Types of Tests Exist?
In backend application testing, there are several types of tests, each with its own purpose and covering different levels of the system. We will focus on the two most important types of tests: unit tests and integration tests (which we will explore with practical examples in the following chapters).
Unit tests
Unit tests are tests that verify individual modules or small sections of code, such as functions or methods, in isolation from the rest of the system. This means that dependencies like databases, external services, or other modules are not considered during testing.
Unit tests help ensure that each individual part of the code works correctly, independently of others.
Note
In unit testing, we simulate the logic needed to check a specific method.
Example
Let's say we have a function that calculates a discount on a product. A unit test would verify that when certain parameters (product price, discount percentage) are provided, the function returns the correct discount value, regardless of other parts of the system.
Integration tests
Integration tests check the interaction between multiple modules or components within a system. They are crucial for ensuring that individual parts of an application work together correctly, exchange data, and perform the tasks expected of the entire system.
Unlike unit tests, integration tests can take into account dependencies such as databases, services, or APIs.
Note
An integration test allows us to test specific functionality within our application rather than just an individual module.
Example
Imagine we have a controller responsible for handling product purchase requests and a service that calculates the order total and interacts with a database to store the information.
An integration test would verify that the purchase request is properly passed through the controller, the service calculates the total, and the order data is correctly saved in the database.
Interim results
Each of these types of tests plays an important role in ensuring the reliability and stability of a backend application.
Unit tests are useful in the early stages of development to verify that each individual component functions correctly, while integration tests help ensure that components interact properly and that the application behaves as expected in real-world scenarios.
What Happens if Tests aren't Written?
In 2012, Knight Capital faced catastrophic consequences when one of their applications was deployed without proper testing. The company specialized in high-frequency trading on the stock exchange, and their new software was released into production without prior validation.
Bugs in the code caused the system to make incorrect trades involving massive sums of money. In just 45 minutes, the system generated losses amounting to $440 million. As a result of this incident, Knight Capital was forced to seek emergency funding and was soon acquired by another company.
This case illustrates how the lack of testing can not only cause application failures but also lead to enormous financial losses for a company. Proper testing could have prevented such a disaster by catching errors during the development phase.
Summary
Testing backend applications is a critical part of the development process that helps avoid errors and maintain high code quality. Unit tests check individual parts of the system, while integration tests verify how they interact. Both types are essential for building a reliable and stable product.
Tudo estava claro?