Posts

Spring Boot WireMock - Testing HTTP clients

Image
In this section, we'll develop a Spring Boot application that connects with a jsonplaceholder client with the help of RestClient and at the end  we will perform integration testing with WireMock  and   JUnit 5 . jsonplaceholder JSONPlaceholder is a free online REST API that we can use whenever we need some fake data. RestClient RestClient is a synchronous client to perform HTTP requests. It is a higher-order API since it performs HTTP requests by using an HTTP client library like the JDK HttpClient, Apache HttpComponents, and others. More Info - click here WireMock WireMock is a popular open-source library for API mock testing. More Info - click here Advantages :      > No load on the remote server.      > Full control over the response.      > Works offline.           JUnit 5 For writing and running unit tests and integration tests for any Java application, the JUnit framework is a great option. It is already included in the spring-boot-starter-test module of Spring Boo

Spring: HandlerInterceptor Example

Image
In this section, we will learn about using Spring HandlerInterceptor in Spring Boot application. The Spring framework includes HandlerInterceptors, which are positioned in between DispatcherServlet and our Controllers. The primary purpose of interceptor is to intercept incoming request and response. It perform some operations on request and response. In Spring, an interceptor can be implemented by implementing HandlerInterceptor Interface. There are numerous uses for interceptors, some of which are mentioned below: 1. Logging Monitoring and troubleshooting running apps require logging. Using a combination of interceptors and Spring Boot's logging framework, we can log all requests, responses, and errors in a single location within an application. 2. Authentication & Authorization Incoming request can be intercepted by the Interceptor & authentication or authorization can be done, if denied then the request does not get delegated to the controller. 3. Request Response Modifi