Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Modular Testing With JUnit | Testing Backend Applications
Spring Boot Backend
course content

Course Content

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

bookModular Testing With JUnit

When we reviewed the application's architecture, we skipped over the test directory, which was intended for tests. Now, we will take a closer look at how to write these tests and what is required for doing so.

Basic Concepts

For testing, we will be using the JUnit library. This is one of the most popular frameworks for unit testing in Java applications. It is easy to use and integrate, especially with Spring Boot.

To use JUnit, we need to add a dependency to our pom.xml file.

Now we are ready to write our first test in our application!

Writing Test

Now we will write a test that is not directly related to our application but will help you understand the concept of testing.

In Spring Boot, tests are typically located in the src/test/java directory, which is automatically created when initializing the project. Here, we can create our test.

Let’s create a class named SimpleTest, and within it, we can create testing method called testSum().

SimpleTest

SimpleTest

copy
123456789101112
public class SimpleTest { // This method tests the sum of two integers. @Test public void testSum() { // Perform the sum of 2 and 2 and store the result in a variable. int result = 2 + 2; // Assert that the result is equal to 4. assertEquals(4, result); } }

The @Test annotation in JUnit indicates that the method is a test and should be executed during the testing process.

Inside the test, an assertion is used: assertEquals(4, result), which checks that the calculation result matches the expected value. If the result does not meet expectations, the test will be considered unsuccessful, signaling a problem in the program's logic.

By running our test, we can see that it has passed successfully!

Rules for Writing Tests

In unit tests, it is important to follow a clear and structured naming system for classes and methods so that tests are readable and easy to maintain.

Class Naming

The name of the class being tested should include the suffix Test. For example, if we are testing a class named BookController, the corresponding test class would be named BookControllerTest.

Method Naming

Test method names should be descriptive, allowing anyone reading them to understand what is being tested. A good naming scheme includes: (What is being tested) _ (Under what conditions) _ (Expected result).

In this test name, testCreateUser indicates that the process of creating a user is being tested. The next part, whenInputIsValid, describes the conditions under which the test is conducted — specifically, when the user input is valid. Finally, shouldReturnCreatedUser specifies the expected outcome — the method should return the created user.

Summary

Unit testing using JUnit allows for the verification of the correctness of individual parts of an application in isolation, ensuring high code quality and system stability when changes are made.

JUnit provides convenient tools for writing, executing, and organizing tests, making it the primary choice for testing Java applications, including Spring Boot.

1. What does the `@Test` annotation do in JUnit?
2. What naming convention should be used for test classes in JUnit?
What does the `@Test` annotation do in JUnit?

What does the @Test annotation do in JUnit?

Select the correct answer

What naming convention should be used for test classes in JUnit?

What naming convention should be used for test classes in JUnit?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt