Posts

A Comprehensive Guide to Context Path Configuration in Spring Web and Spring WebFlux

Image
Introduction Team is working on spring boot version upgrade to 3.x and java to java 17. We noticed a few changes required for Context Path. The context path plays a crucial role in web applications, serving as the prefix to all URLs within the application. Configuring the context path correctly is essential, especially in scenarios where multiple applications are deployed on the same server. In this guide, we'll explore how to configure the context path in both Spring Web and Spring WebFlux app  

A Comprehensive Guide to Using H2 In-Memory Database in Unit Tests

Image
Introduction Unit testing is an integral part of software development, ensuring that individual components of an application function as expected in isolation. When testing components that interact with databases, using an in-memory database is a common approach to achieve fast and reliable tests. In this guide, we'll explore how to use the H2 in-memory database in your unit tests with Spring Boot. Step 1: Add H2 Dependency First, ensure that you have the H2 database dependency added to your  

why @WithMockUser still got 401 unauthorized

Image
@AutoConfigureMockMvc for none reactive spring application, the Servlet API and Servlet containers, spring-boot-starter-web, please use MockMvc. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @AutoConfigureMockMvc @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class IntegrationTestsBase { @Autowired protected MockMvc mockMvc; ... mockMvc.perform(  

Leveraging Mock Server in Spring Boot Integration Tests

Image
Introduction: Integration testing is crucial for ensuring the robustness and reliability of Spring Boot applications. However, testing against real external services can introduce complexity and dependencies, leading to slower and less predictable tests. In this tutorial, we'll explore how to use a Mock Server in Spring Boot integration tests to simulate external dependencies, enabling faster and more deterministic testing. What is a Mock Server? A Mock Server is a tool that allows developers t  

Spring Boot Retry Example

Image
we encountered an unusual SSL handshake error affecting a subset of our applications. The issue exhibited a sporadic nature, with approximately a 20% reproduction rate, and intriguingly, a retry attempt consistently resolved it. For a straightforward workaround, we aim to implement the retry logic elegantly. Spring Retry module provides the ability, let's dig the usage of it and apply into our application. Maven spring-retry dependency <dependencies> <dependency> <groupId>org.spri