Spring Boot Resources

Spring Boot - Unit Testing



Unit Testing is a one of the testing done by the developers to make sure individual unit or component functionalities are working fine.

In this tutorial, we are going to see how to write a unit test case by using Mockito and Web Controller.

Example - Using Mockito for unit Testing a Web Controller

For injecting Mockito Mocks into Spring Beans, we need to add the Mockito-core dependency in our build configuration file.

In this chapter, we are going to see how to use Mockito in Spring Boot application.

First, download the Spring Boot project from Spring Initializer page www.start.spring.io.

Creating Mockito Project

Maven users can add the following dependency in your pom.xml file.

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-core</artifactId>
</dependency>

Gradle users can add the following dependency in the build.gradle file.

compile group: 'org.mockito', name: 'mockito-core', version: '5.13.0'
testCompile('org.springframework.boot:spring-boot-starter-test')

The code to write a Service class which contains a method that returns the String value is given here.

ProductService.java

package com.tutorialspoint.mockito;

import org.springframework.stereotype.Service;

@Service
public class ProductService {
   public String getProductName() {
      return "Honey";
   } 
}

Now, inject the ProductService class into another Service class file as shown.

OrderService.java

package com.tutorialspoint.mockito;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
   @Autowired
   ProductService productService;

   public OrderService(ProductService productService) {
      this.productService = productService;
   }
   public String getProductName() {
      return productService.getProductName();
   }
}

The main Spring Boot application class file is given below −

MockitoApplication.java

package com.tutorialspoint.mockitodemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MockitoApplication {
   public static void main(String[] args) {
      SpringApplication.run(MockitoApplication.class, args);
   }
}

Then, configure the Application context for the tests. The @Profile("test") annotation is used to configure the class when the Test cases are running.

ProductServiceTestConfiguration.java

package com.tutorialspoint.mockito;

import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Profile("test")
@Configuration
public class ProductServiceTestConfiguration {
   @Bean
   @Primary
   ProductService productService() {
      return Mockito.mock(ProductService.class);
   }
}

Now, you can write a Unit Test case for Order Service under the src/test/java package.

MockitoApplicationTests.java

package com.tutorialspoint.mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class MockitoApplicationTests {
   @Autowired
   private OrderService orderService;
   
   @Autowired
   private ProductService productService;

   @Test
   public void whenUserIdIsProvided_thenRetrievedNameIsCorrect() {
      Mockito.when(productService.getProductName()).thenReturn("Mock Product Name");
      String testName = orderService.getProductName();
      assertEquals("Mock Product Name", testName);
   }
}

The complete code for build configuration file is given below.

Maven pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.5.6</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>mockito</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>mockito</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Gradle build.gradle

buildscript {
   ext {
      springBootVersion = '3.5.6'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter')
   compile group: 'org.mockito', name: 'mockito-core', version: '5.13.0'
   testCompile('org.springframework.boot:spring-boot-starter-test')
}   

Output

You can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands.

For Maven, you can use the command as shown −

mvn clean install 

You can see the test results in console window.

[INFO] Scanning for projects...
[INFO] 
[INFO] [1m---------------------< [0;36mcom.tutorialspoint:mockito[0;1m >---------------------[m
[INFO] [1mBuilding mockito 0.0.1-SNAPSHOT[m
[INFO]   from pom.xml
[INFO] [1m--------------------------------[ jar ]---------------------------------[m
[INFO] 
[INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mmockito[0;1m ---[m
[INFO] Copying 1 resource from src\main\resources to target\classes
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO] 
[INFO] [1m--- [0;32mcompiler:3.14.0:compile[m [1m(default-compile)[m @ [36mmockito[0;1m ---[m
[INFO] Recompiling the module because of [1madded or removed source files[m.
[INFO] Compiling 4 source files with javac [debug parameters release 21] to target\classes
[INFO] 
[INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mmockito[0;1m ---[m
[INFO] skip non existing resourceDirectory D:\Projects\mockito\src\test\resources
[INFO] 
[INFO] [1m--- [0;32mcompiler:3.14.0:testCompile[m [1m(default-testCompile)[m @ [36mmockito[0;1m ---[m
[INFO] Recompiling the module because of [1mchanged dependency[m.
[INFO] Compiling 1 source file with javac [debug parameters release 21] to target\test-classes
[INFO] 
[INFO] [1m--- [0;32msurefire:3.5.4:test[m [1m(default-test)[m @ [36mmockito[0;1m ---[m
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tutorialspoint.mockito.[1mMockitoApplicationTests[m
11:56:42.986 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.tutorialspoint.mockito.MockitoApplicationTests]: MockitoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
11:56:43.115 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.tutorialspoint.mockito.MockitoApplication for test class com.tutorialspoint.mockito.MockitoApplicationTests

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.6)

2025-09-30T11:56:43.588+05:30  INFO 43764 --- [mockito] [           main] c.t.mockito.MockitoApplicationTests      : Starting MockitoApplicationTests using Java 21.0.6 with PID 43764 (started by mahes in D:\Projects\mockito)
2025-09-30T11:56:43.589+05:30  INFO 43764 --- [mockito] [           main] c.t.mockito.MockitoApplicationTests      : The following 1 profile is active: "test"
Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
WARNING: A Java agent has been loaded dynamically (C:\Users\mahes\.m2\repository\net\bytebuddy\byte-buddy-agent\1.17.7\byte-buddy-agent-1.17.7.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
2025-09-30T11:56:45.108+05:30  INFO 43764 --- [mockito] [           main] c.t.mockito.MockitoApplicationTests      : Started MockitoApplicationTests in 1.831 seconds (process running for 2.769)
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.435 s -- in com.tutorialspoint.mockito.[1mMockitoApplicationTests[m
[INFO] 
[INFO] Results:
[INFO] 
[INFO] [1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0[m
[INFO] 
[INFO] 
[INFO] [1m--- [0;32mjar:3.4.2:jar[m [1m(default-jar)[m @ [36mmockito[0;1m ---[m
[INFO] Building jar: D:\Projects\mockito\target\mockito-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] [1m--- [0;32mspring-boot:3.5.6:repackage[m [1m(repackage)[m @ [36mmockito[0;1m ---[m
[INFO] Replacing main artifact D:\Projects\mockito\target\mockito-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to D:\Projects\mockito\target\mockito-0.0.1-SNAPSHOT.jar.original
[INFO] 
[INFO] [1m--- [0;32minstall:3.1.4:install[m [1m(default-install)[m @ [36mmockito[0;1m ---[m
[INFO] Installing D:\Projects\mockito\pom.xml to C:\Users\mahes\.m2\repository\com\tutorialspoint\mockito\0.0.1-SNAPSHOT\mockito-0.0.1-SNAPSHOT.pom
[INFO] Installing D:\Projects\mockito\target\mockito-0.0.1-SNAPSHOT.jar to C:\Users\mahes\.m2\repository\com\tutorialspoint\mockito\0.0.1-SNAPSHOT\mockito-0.0.1-SNAPSHOT.jar
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time:  8.257 s
[INFO] Finished at: 2025-09-30T11:56:47+05:30
[INFO] [1m------------------------------------------------------------------------[m
Advertisements