Writing Unit Tests with JUnit and Mockito in spring boot

This guide provides a detailed walkthrough for writing unit tests with JUnit and Mockito. Here we’ll discuss the dependencies required, structure of the test case, and the step-by-step approach to write the test. In this particular example, we’ll be testing a method that creates a user. Writing Unit Tests with JUnit and Mockito in spring boot.

Writing Unit Tests with JUnit and Mockito in spring boot

Required Dependencies

To write and run the unit tests, you need to include the following dependencies in your build.gradle:

//Spring test

testImplementation ‘org.springframework.boot:spring-boot-starter-test’

testImplementation ‘org.springframework:spring-test’

// JUnit 5

testImplementation ‘org.junit.jupiter:junit-jupiter-api:5.7.2’

testImplementation ‘org.junit.jupiter:junit-jupiter-engine:5.7.2’

// Mockito

testImplementation ‘org.mockito:mockito-core:4.1.0’

testImplementation ‘org.mockito:mockito-junit-jupiter:4.1.0’


// ByteBuddy (required by Mockito for creating mocks)

testImplementation ‘net.bytebuddy:byte-buddy:1.11.16’

Test Class Structure

Our test class UserProfileServiceTest is responsible for testing the methods inside the UserProfileService class.

The class starts with the @ExtendWith(MockitoExtension.class) annotation which instructs JUnit 5 to enable Mockito’s annotation-driven capabilities.

We create mocks for dependencies using the @Mock annotation, and the @InjectMocks annotation is used to create an instance of the class that needs to be tested (UserProfileService in this case) and inject the mocks as dependencies.

Setting up the Test Case

The @BeforeEach annotation is used to specify a method that should be executed before each @Test method in the class. Here we use MockitoAnnotations.initMocks(this) to initialize objects annotated with @Mock for each test method.


Writing the Test Case


Next, we have the actual test case, annotated with @Test. In this case, we are testing the createUser method of UserProfileService. The unit test typically follows the Arrange-Act-Assert (AAA) pattern.


We start by creating our required inputs and configuring the behavior of our mocks. We set up a UserCreateRequest, expected UserOrganization, Squad, and other required objects.

For example, we instruct Mockito what to return when the hasUserProfilePrivilege() method of the PrivilegeHandler class is called:



Similarly, we set up behavior for other mocked objects.


Next, we call the actual method we are testing and store the result:

ResponseEntity<?> actualResponse = userProfileService.createUser(authHeader, userRequest);


Finally, we assert that the method’s output is as expected. We check if the returned ResponseEntity‘s status code is HttpStatus.OK and if the UserOrganization object returned in the body of the ResponseEntity has the same properties as the expected UserOrganization.


And that’s it! With JUnit and Mockito, you can easily write unit tests to ensure your code behaves as expected under different conditions. You should write a separate @Test method for each condition you wish to test. Remember, each unit test should be independent and test only one condition or functionality.

Next Steps

For more advanced use cases, you might need to use other features of Mockito, like verify() to check that certain methods have been called, ArgumentCaptor to capture method arguments for further assertions, etc. Furthermore, consider using @Nested for better organization of your tests if your test class starts to grow.


public class ApplicationTestingTest {
    private AttachmentSourceBean attachmentSourceBean;

    private AuthServiceProxy authServiceProxy;
    private INTTRABookingRequestRepository inttraBookingRequestRepository;

    public void setup() {

    void testGetCarrierByCarrierInfo(){
        String carrierName = "TestCarrier";
        String polCountryCode = "US";
        ResponseEntity responseEntity = carrierService.getCarrierByCarrierInfo(carrierName,polCountryCode);

    void testGetCarrierByCarrierInfoWithException(){

        ResponseEntity responseEntity = carrierService.getCarrierByCarrierInfo(null,null);


    void getBookingList() throws AccessDeniedException {
       Iterable<CarrierBooking> result = carrierService.getBookingList();

    void getBookingListWithException() throws AccessDeniedException {



    void TestUpDateBooking() throws AccessDeniedException {

        INTTRABookingRequest inttraBookingRequest = createDemoBookingRequest();

        DefaultResponse response = carrierService.updateBooking(inttraBookingRequest);
    void TestUpDateBookingException() throws AccessDeniedException {

        INTTRABookingRequest inttraBookingRequest = createDemoBookingRequest();

    void TestUpDateBookingWith() throws AccessDeniedException {




You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *