Unit testing/Component testing
With "units" respectively "components" in OOP refer to methods. What does this mean?
Every method has to be written and tested by the developer and they did it for a long time manually. In the meantime, testing can be automated: For this purpose unit tests are written for each method. The number of unit tests depends on the number of code pathes in the method. Each code path should be tested.
Oops! That sounds like a lot of work. Why should we do this?
Save time
Of course, additional code has to be written for the tests first. But these are quickly created:
public class SavingsAccount extends Account { private double interestRate; SavingsAccount(double initialCredit, double interestRate) { super(initialCredit); this.interestRate = interestRate; } public int transaction(double amount) { // this method is tested below if (balance + amount < 0) return -1; else { balance += amount; return 0; } } }
This class has to be tested
All you have to do is call the method with the appropriate parameters and check for the correct return value:
import static org.junit.jupiter.api.Assertions.assertEquals; class SavingsAccountTest { // contains 2 tests @org.junit.jupiter.api.Test void transactionTest1() { SavingsAccount savingsAccount = new SavingsAccount(50, 0); // create object assertEquals(0, savingsAccount.transaction(-50)); // withdraw of money allowed assertEquals(0, savingsAccount.getBalance()); // account is now empty } @org.junit.jupiter.api.Test void transactionTest2() { SavingsAccount savingsAccount = new SavingsAccount(50, 0); assertEquals(-1, savingsAccount.transaction(-100)); // try to overdraw assertEquals(50, savingsAccount.getBalance()); // not possible } }
Testclass with 2 unit tests - assertEquals() compares return value with expected value
But now the tests run automatically in a short time and even with small projects you don't have to constantly manually operate any buttons in the program interface and laboriously interpret the result.

Test results in IntelliJ IDEA
Side effects can also easily occur unnoticed during refectorings or maintenance work. This means that an error occurs at a different point in the code, since normally not the whole program is tried out manually every time, but only the changed functionality.
The error is noticed late, maybe even at the customer. Now the connection between the change and the new error has to be determined laboriously in order to eliminate it.
In contrast, all unit tests run completely after each refactoring, the whole program is tested automatically, side effects are noticed immediately and can easily be associated with the change.
High quality of code
Unit testing improves the quality of the code. It identifies every
defect that may have come up before code is sent further for integration
testing. Writing tests makes developers think harder
about the problem and makes them write better
code.
Code documentation
Unit testing provides documentation of the system. Developers inspect unit tests for getting an overview about the functionality of classes and how to use it.