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

unittest Article's
30 articles in total
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 1/3)
Favicon
What is the Order of Importance for Unit, Integration, and Acceptance Tests in Software Development?
Favicon
Top 17 Best Unit Testing Tools
Favicon
JUnit Testing: A Comprehensive Guide to Unit Testing in Java
Favicon
Crafting Effective Unit Tests for Generative AI Applications
Favicon
Harder, Better, Faster, Stronger Tests With Fixtures
Favicon
How To Test Your JavaScript Application With Jest Framework?
Favicon
Effective Strategies for Writing Unit Tests with External Dependencies like Databases and APIs
Favicon
Debugging Laravel Routes in Testing
Favicon
Is Unit Test really a MUST?
Favicon
Let's Learn Unit Testing in Python with pytest! 🚀
Favicon
An opinionated testing approach for VueJS
Favicon
PHP: Should I mock or should I go?
Favicon
Implementing Unit Testing in ReadmeGenie
Favicon
8.Angular Unit Testing: A Step-by-Step Approach
Favicon
“Why Unit Testing Is Not an Option, But a Duty”
Favicon
Early Raises $5M to Transform Software Development
Favicon
Smth about Unittests
Favicon
Unit Testing in Laravel: A Practical Approach for Developers
Favicon
Assert with Grace: Custom Soft Assertions using AssertJ for Cleaner Code
Favicon
Writing Integration And Unit Tests for a Simple Fast API application using Pytest
Favicon
End-To-End Testing for Java+React Applications
Favicon
Writing XUnit Tests without Object Arrays
Favicon
Comprehensive Testing in .NET 8: Using Moq and In-Memory Databases
Favicon
Test-Driven Development (TDD) in Front-end.
Favicon
Unit testing
Favicon
jUnit - Testes unitários em Java
Favicon
Testing Spring Boot Applications: Unit, Integration, and Mocking — A Comprehensive Guide
Favicon
Let’s Make Jest Run Much Faster
Favicon
Test-Driven Development

Featured ones: