Logo

dev-resources.site

for different kinds of informations.

Testing Spring Boot Applications: Unit, Integration, and Mocking — A Comprehensive Guide

Published at
10/30/2024
Categories
integrationtest
mock
test
unittest
Author
igventurelli
Author
12 person written this
igventurelli
open
Testing Spring Boot Applications: Unit, Integration, and Mocking — A Comprehensive Guide

Testing is a critical aspect of software development, ensuring the reliability and stability of your applications. In this blog post, we'll dive into the essentials of testing Spring Boot applications using JUnit and Mockito. We'll cover unit tests, integration tests, and mocking, providing examples using MockServer and Testcontainers.

What is Spring Boot?

Spring Boot is an open-source framework designed to simplify the development of Java applications. It provides a comprehensive infrastructure for developing microservices and standalone applications with minimal configuration. Spring Boot comes with embedded servers, making it easy to create and deploy applications quickly.

What is a Unit Test?

A unit test is a type of software testing where individual units or components of a software are tested in isolation from the rest of the application. The goal is to ensure that each unit of the software performs as expected. Unit tests are usually automated and help identify issues early in the development process.

Example of a Unit Test in Spring Boot

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void testCalculateSum() {
        int result = myService.calculateSum(5, 10);
        // myService.calculateSum() doesn't have external integration/dependencies, this way we're testing the unit calculateSum() and only that, making this test a Unit Test
        assertEquals(15, result);
    }
}
Enter fullscreen mode Exit fullscreen mode

What is an Integration Test?

An integration test is a type of testing where individual units are combined and tested as a group. The purpose is to verify the interactions between different parts of the application and ensure they work together correctly. Integration tests often involve external components like databases, message brokers, and web services.

Example of an Integration Test in Spring Boot

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MyControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testGetEndpoint() throws Exception {
        mockMvc.perform(get("/api/data"))
                .andExpect(status().isOk())
                .andExpect(content().string("Hello, World!"));

        // this way we're testing the entire flow (with all the external integrations and dependencies the route /api/data may have, making this a Integration Test
    }
}
Enter fullscreen mode Exit fullscreen mode

What is JUnit?

JUnit is a popular testing framework for Java, used to write and run repeatable tests. It provides annotations to identify test methods, setup and teardown routines, and assertions to check expected results.

What is Mocking and Mockito?

Mocking is a technique used in unit testing to simulate the behavior of real objects. This allows you to test components in isolation without relying on external dependencies. Mockito is a popular mocking framework for Java, making it easy to create and configure mock objects.

Example of Mocking with Mockito

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {

    @InjectMocks
    private MyService myService;

    @Mock
    private MyRepository myRepository;

    @Test
    public void testFindById() {
        when(myRepository.findById(1L)).thenReturn(Optional.of(new MyEntity(1L, "Test")));
        MyEntity result = myService.findById(1L);
        assertEquals("Test", result.getName());

        // this way we're mocking the repository layer, making the test still unit, even with an external dependency
    }
}
Enter fullscreen mode Exit fullscreen mode

Using MockServer and Testcontainers

MockServer and Testcontainers are powerful tools for creating integration tests that involve external services.

Example of MockServer

MockServer allows you to mock HTTP requests and responses.

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceIntegrationTest {

    private ClientAndServer mockServer;

    @Before
    public void startServer() {
        mockServer = ClientAndServer.startClientAndServer(1080);
    }

    @After
    public void stopServer() {
        mockServer.stop();
    }

    @Test
    public void testExternalServiceCall() {
        mockServer.when(
                request()
                        .withMethod("GET")
                        .withPath("/external-api"))
                .respond(
                        response()
                                .withStatusCode(200)
                                .withBody("Mocked Response"));

        String result = myService.callExternalService();
        assertEquals("Mocked Response", result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Example of Testcontainers

Testcontainers provides lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

@RunWith(SpringRunner.class)
@SpringBootTest
@Testcontainers
public class MyRepositoryIntegrationTest {

    @Container
    public static PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:latest")
            .withDatabaseName("test")
            .withUsername("user")
            .withPassword("password");

    @Autowired
    private MyRepository myRepository;

    @Test
    public void testDatabaseInteraction() {
        MyEntity entity = new MyEntity();
        entity.setName("Test");
        myRepository.save(entity);

        Optional<MyEntity> result = myRepository.findById(entity.getId());
        assertTrue(result.isPresent());
        assertEquals("Test", result.get().getName());
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Testing Spring Boot applications effectively requires understanding and implementing various types of tests, such as unit tests, integration tests, and mocking. JUnit and Mockito are essential tools in this process, and frameworks like MockServer and Testcontainers can significantly enhance your testing capabilities. By incorporating these practices, you can ensure your Spring Boot applications are robust, reliable, and maintainable.


Let’s connect!

📧 Don't Miss a Post! Subscribe to my Newsletter!
➡️ LinkedIn
🚩 Original Post
☕ Buy me a Coffee

mock Article's
30 articles in total
Favicon
FAQ — Bloomer Mock Data Generator
Favicon
Best Practices: How to Make Your API Smarter and More Flexible
Favicon
Testing ReactJS Context - A Guide with test-doubles
Favicon
The beauty of MSW
Favicon
Realistic data Generation Using Bloomer Mock
Favicon
Testing Spring Boot Applications: Unit, Integration, and Mocking — A Comprehensive Guide
Favicon
How to debug your Python mocks or imports
Favicon
Is there any option for Mock EncryptAsync() in Azure.Security.KeyVault.Keys.Cryptography
Favicon
Learn to Simulate Online APIs Without Server Infrastructure
Favicon
Diferenças entre o jest.spyOn e jest.mock
Favicon
Mock Class Constructor in Jest Test with Mocking Partials
Favicon
You stub/mock incorrectly
Favicon
Step-by-Step Tutorial on Setting Up a Mock Server Using Postman
Favicon
Mocking ES6 Class Instances with Jest
Favicon
Create free mock apis with unlimited storage
Favicon
Free API server with unlimited access
Favicon
Handle Variables and Dynamic Values with `jest.fn()`
Favicon
A way to mock PHP internal functions with xepozz/internal-mocker
Favicon
Mock vs Stub vs Fake: Understand the difference
Favicon
Mock modules properly with Jest and Typescript
Favicon
Simplified Strategies for Mocking API Calls
Favicon
Fastify Meets WireMock: External Service Mocking
Favicon
OpenAI API Mock for Devs on a Budget
Favicon
Mock S3 for AWS SDK for JavaScript (v3)
Favicon
Simplifying Kafka Testing in Python: A Mockafka-py Tutorial
Favicon
When to use Mock API?
Favicon
Datafaker: Simplifying Test Data Generation for Java and Kotlin
Favicon
NestJS: Mocking Databases for Efficient Tests
Favicon
Understanding Mocks, Stubs, and Fakes in Software Testing
Favicon
How to Typescript to JSON with Butlermock

Featured ones: