Posts

Showing posts with the label Junit

Spring Boot - Testing Spring Web Controller with @WebMvcTest - Example

Image
  In this section, we will learn how to test Spring Web Controller with  @WebMvcTest .   1. @WebMvcTest Instead of bootstrapping the entire application context for every test,  @WebMvcTest allows us to initialize only the parts of the Application context that are necessary for our Spring MVC web layer.  This allows us to focus on testing the controllers, views, and related components. Following beans will be scanned while using @WebMvcTest: @Controller @ControllerAdvice @JsonComponent Converter/GenericConverter Filter WebMvcConfigurer HandlerMethodArgumentResolver Regular @Component, @Service or @Repository beans are not scanned when using this annotation. This approach not only speeds up the testing process but also ensures a focused and efficient testing environment.  This approach is also known as "slicing" the application context. The annotation can be used to test a single controller by passing it as an attribute, for example @WebMvcTest(SomeController.class). @WebMvcTes

Spring Boot + Mockito simple application with 100% code coverage

Image
In this article, we will show you a simple  Spring Boot example to demonstrate test methods for Controllers, Service, and Repository, And code coverage analysis using the EclEmma plugin. Technologies used: Spring Boot 2.6.4 Mockito 3.11.2 Maven 3+ EclEmma plugin Junit 5 Java 17 A quick overview of  Spring Boot, Mockito, and EclEmma plugin Spring boot: Spring boot to develop REST web services and microservices. Spring Boot has taken the Spring framework to the next level. It has drastically reduced the configuration and setup time required for spring projects. We can set up a project with almost zero configuration and start building the things that actually matter to your application. Mockito: Mockito is a mocking framework, a JAVA-predicated library that is utilized for efficacious unit testing of JAVA applications. Mockito is utilized to mock interfaces so that a dummy functionality can be integrated into a mock interface that can be utilized in unit testing. EclEmma : EclEm