Spring boot API Unit Test cases

JUnit5 with Mockito Test Cases for Spring Boot 3 APIs in Aviation Industry

JUnit5 with Mockito Test Cases for Spring Boot 3 APIs in Aviation Industry

Table of Contents

  • 1. Introduction
  • 2. Overview of Spring Boot 3 and JUnit5
  • 3. Introduction to Mockito for Unit Testing
  • 4. Flight Booking System Architecture
  • 5. Setting Up the Environment
  • 6. Writing Test Cases with JUnit5 and Mockito
  • 7. Code Examples
  • 8. Conclusion
  • 9. Download Sample Project (ZIP)

1. Introduction

This white paper demonstrates how to write unit tests for a Spring Boot 3-based Flight Booking System using JUnit5 and Mockito. The goal is to ensure the correctness of controllers, services, and DAO components using effective unit testing strategies.

2. Overview of Spring Boot 3 and JUnit5

Spring Boot 3 offers enhanced performance and streamlined APIs. JUnit5 provides a flexible and powerful testing framework. Mockito simplifies mocking dependencies for unit testing.

3. Introduction to Mockito for Unit Testing

Mockito is a Java-based testing library used to create mock objects for simulating behaviors of real objects in unit tests. It is widely used in Spring Boot applications for isolating test cases.

4. Flight Booking System Architecture

The flight booking system consists of the following layers:

  • Controller: Handles HTTP requests and responses.
  • Service: Contains business logic.
  • DAO (Data Access Object): Manages database operations.

Example Components

  • FlightController: Manages flight booking API endpoints.
  • FlightService: Contains flight management business logic.
  • FlightRepository: Handles database queries.

5. Setting Up the Environment

Install Java 17+, Spring Boot 3, and add JUnit5 and Mockito dependencies in pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

6. Writing Test Cases with JUnit5 and Mockito

Controller Layer Test

@WebMvcTest(FlightController.class)
public class FlightControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @MockBean
  private FlightService flightService;

  @Test
  public void testGetFlights() throws Exception {
    List<Flight> flights = List.of(new Flight(1L, "NYC", "LAX"));
    Mockito.when(flightService.getAllFlights()).thenReturn(flights);

    mockMvc.perform(get("/flights"))
           .andExpect(status().isOk())
           .andExpect(jsonPath("$[0].source").value("NYC"));
  }
}

Service Layer Test

@SpringBootTest
public class FlightServiceTest {

  @Mock
  private FlightRepository flightRepository;

  @InjectMocks
  private FlightService flightService;

  @Test
  public void testGetAllFlights() {
    List<Flight> flights = List.of(new Flight(1L, "NYC", "LAX"));
    Mockito.when(flightRepository.findAll()).thenReturn(flights);

    List<Flight> result = flightService.getAllFlights();
    Assertions.assertEquals(1, result.size());
  }
}

DAO Layer Test

@DataJpaTest
public class FlightRepositoryTest {

  @Autowired
  private FlightRepository flightRepository;

  @Test
  public void testSaveFlight() {
    Flight flight = new Flight(null, "NYC", "LAX");
    Flight savedFlight = flightRepository.save(flight);

    Assertions.assertNotNull(savedFlight.getId());
  }
}

7. Code Examples

Complete examples with additional test cases are included in the downloadable ZIP file.

8. Conclusion

JUnit5 and Mockito provide robust testing capabilities for Spring Boot applications. By writing effective unit tests, you can ensure the stability and reliability of your Flight Booking System.

9. Download Sample Project

Download the sample project here.

Comments

Popular posts from this blog

About naveen gaayaru

About Naveen G

Boosting Small Businesses in Your Community