Posts

Spring Boot Mockito - @Mock & @InjectMocks Example

Image
In this tutorial, we will learn about the  @Mock and @InjectMocks  annotation of Mockito framework, and at the end, we will see how the Spring Boot application can utilize these  annotation for unit testing with JUnit 5 . Therefore, read the post till the end. Mockito Mocking framework Mockito is used for efficient unit testing in Java-based applications. It is used in conjunction with  testing framework JUnit. We construct a mock with Mockito, instruct it on what to do when certain methods are called on it, and then utilize the mock instance rather than the real one in our test. @Mock A mock object is created for a specified class or interface using the @Mock annotation. It gives Mockito instructions on how to make a proxy object that behaves just like the original object. We can mimic the actions of dependencies without calling their real methods by using @Mock . Here’s an example of using @Mock : @ExtendWith(MockitoExtension. class ) public class UserServiceTests { @Mock pr

@DataR2dbcTest - Testing Spring Data R2DBC components

Image
In this section, we will learn how to test  Data R2DBC components  with @DataR2dbcTest in the Spring Boot application. About  @DataR2dbcTest Instead of bootstrapping the entire application context for every test,  @DataR2dbcTest  allows us to initialize only the parts of the Application context that are relevant to Data R2DBC components tests. Regular @Component , @Service or @Controller 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 supports the following attributes: excludeAutoConfiguration :  Auto-configuration exclusions that should be applied for this test. excludeFilters : beans that would ordinarily be added to the application context can be filtered using a set of exclude filters. includeFilters : beans that would otherwise be filtered and added to the application e