TDD : Test Driven Development
Test-driven development (TDD) is a software development process in which developers write automated tests before writing the actual code. Code reliability, maintainability, and bug-freeness are all made possible through TDD. We’ll go over examples and tools for test-driven development with Spring Boot in this response.
Setup Environment
You must have a development environment set up before you can begin using test-driven development in Spring Boot. From the Spring website, you can get the most recent version of the Spring Tool Suite (STS). You can start a new Spring Boot project after downloading and installing STS.
Write Test
You can start developing tests for your application as soon as you have established a new Spring Boot project. You must develop a Java class with test methods in order to create a test in Spring Boot. The functionality of the code you intend to write should be tested using these test methods. Here is an illustration of a Spring Boot test class:
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyMathClassTest {
@Test
public void testAddition() {
int num= 2;
int num2= 3;
int result = num + num2;
assertEquals(result, 5);
}
}
In this instance, we’ve written a test function called testAddition() that verifies how well two integers add up. This method has been identified as a test method using the @Test annotation. This is a Spring Boot test, which is also indicated by the @SpringBootTest annotation. The assertion that the result of the addition equals 5 is made using the assertEquals() function.
Running the Test
Once the test is written, you may run it to see if it succeeds. Right-click the test class and choose “Run As” > “JUnit Test” to execute the test. The test output should appear on the console.
If the test is successful, you can move on to writing the code to put the functionality you tested into use. If the test fails, you must make changes to your code until it succeeds.
Implementing the actual Code
Once your test passes, you can implement the code that provides the functionality you have tested. Here is an illustration of how the addition method is used:
public class MyMathClass {
public int add(int num1, int num2) {
return num1 + num2;
}
}
In this example, we have created an add() method that adds two integers and returns the result.
Refactoring
You should refactor the code once you implement it to make sure it is scalable and manageable. Refactoring entails enhancing the code’s structure and design without affecting its functionality. An illustration of restructuring the add() method is shown here:
public class Example {
public int add(int... numbers) {
int result = 0;
for (int number : numbers) {
result += number;
}
return result;
}
}
In this example, the add() method has been refactored to take any amount of integer inputs. This increases the method’s adaptability and scalability.
Useful Tools
Spring Boot comes with built-in testing support. It includes the following tools:
- JUnit: a unit testing framework for Java
- Mockito: a mocking framework for Java
- AssertJ: a fluent assertion library for Java
You can use these tools to write and run tests for your application.
Common Mistakes
TDD has some frequent traps, nevertheless, that developers should be aware of. However, there are common pitfalls that developers can fall into while doing TDD. In this answer, we will discuss some of the common pitfalls and how to refactor code to avoid them.
Writing Too Many Test Cases:
We should avoid writing too many test cases. Like in above example, we wrote a single test case to check our simple method add(). Simple methods like these should not be checked in multiple tests.
Testing Implementation Details:
While using TDD, testing implementation details rather than behaviour is another typical mistake. As a result, tests may become brittle and fail when the code is changed. Writing tests that evaluate the behaviour of the code rather than its implementation will help to prevent this.
Take the code below, for instance, which adds up all the even numbers in an array.
public class MyCalculator {
public int sumEven(int[] numbers) {
int sum = 0;
for (int number : numbers) {
if (number % 2 == 0) {
sum += number;
}
}
return sum;
}
}
You can write its test as case as below
public class MyCalculatorTest {
@Test
public void testSumEven() {
ArrayCalculator calculator = new ArrayCalculator();
int[] numbers = { 1, 2, 3, 4, 5, 6 };
assertEquals(calculator.sumEven(numbers), 12);
}
}
In this example, we have written a test that ensures the sumEven() method of the MyCalculator class returns the correct sum of even numbers in an array. This test focuses on the behaviour of the code rather than its implementation.
Avoiding Code Refactoring:
In TDD, whenever a code change happens, test changes are also required mostly. But We should not avoid the refactoring of the code if it simplifies the testing. This is a part of maintenance process which team should keep doing with each change.