Member-only story

Unit testing is awesome

theoneamin
3 min readJun 19, 2023

Testing is often seen as a tedious and time-consuming task by many developers. However, in this article, I aim to change that perspective. I firmly believe that tests are a fundamental part of the development process, and not only do they bring numerous benefits, but they also foster a positive attitude towards software quality. In this post, we will explore few of the many reasons why writing tests is important, with a focus on Spring Boot development. Along the way, we’ll provide some code examples to illustrate the basic concepts.

Find bugs before they find you

One of the primary reasons to write tests is to catch bugs early in the development cycle. Tests act as a safety net, allowing you to validate your code’s correctness continuously. By writing unit tests using frameworks like JUnit and Spring Boot Test, you can isolate and verify the behaviour of individual components or methods.

Say we have a user service class which hold our user management functionality. We can write a simple test as shown below to to ensure that our createUser method does what it’s meant to do.

@SpringBootTest
public class UserServiceTest {

@Autowired
private UserService userService;

@Test
public void testGetUserById() {
// Arrange
User user = new User("John", "Doe");
userService.createUser(user);

// Act
User retrievedUser = userService.getUserById(user.getId());

// Assert
assertEquals(user…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

theoneamin
theoneamin

No responses yet

Write a response