BLOG

Tests - important code

Tests in programming are crucial because they ensure that the code works correctly. They help identify errors early on, saving time and resources. Good tests improve the quality of the software and facilitate changes, ensuring that new features do not introduce bugs.

E2E, Integration test, Unit test

Tests

End-to-End (E2E, functional) tests – tests that check entire business functionalities, starting from the graphical user interface and conducted by a manual tester or a robot.

Integration tests – tests that verify the operation of connected individual modules, for example, testing the program’s behavior when connecting to a database or external services.

Unit tests – tests that verify the correctness of individual elements of the program. They check only the business logic without connecting to external components. Popular frameworks such as JUnit5 and Mockito 2 are used to write them. The first is used for running tests and creating assertions, while the latter is primarily used for creating mocks—special objects that simulate the behavior of real objects.

Stub, Mock, Spy

Types of auxiliary objects used in unit tests

A stub is an object containing a sample implementation of the code we want to test. It allows us to define the behavior of that object. This is a manual implementation of dependency data, which may be sufficient in small projects.

A mock is an object just like a stub, simulating the behavior of a real object; however, it provides the ability to verify its behavior and offers significantly more functionality. It is created using the Mockito library.

A spy is a wrapper around a real object whose behavior can be monitored without altering its functionality. It is a hybrid object because it can be partially a mock and partially a real object.

TDD, Refactor

Test Driven Development

TDD  is a programming approach that involves writing a unit test first and then implementing the functionality to make that test pass. The diagram above illustrates how one cycle of this methodology should correctly look: RED – we write a unit test that should fail, GREEN – we write code (the minimum possible!) to make the test written in the previous step pass, REFACTOR – we improve the written code (if necessary).