Spring Boot EasyMock Tutorial


EasyMock is a testing framework for Java that allows you to create mock objects for interfaces and classes. It simplifies the creation of test doubles for unit testing. In this tutorial, we will cover how to use EasyMock in a Spring Boot application to test CRUD (Create, Read, Update, Delete) operations.

To learn more about EasyMock, check out this guide: EasyMock.


  1. Java Development Kit (JDK) 17 or later
  2. Apache Maven installed
  3. An IDE like IntelliJ IDEA or Eclipse

Step 1: Create a Spring Boot Project

You can create a Spring Boot project using Spring Initializr or your IDE.

Using Spring Initializr

  1. Go to Spring Initializr.
  2. Select the following options:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.0.0 or later
    • Group: com.example
    • Artifact: easymock-demo
    • Name: easymock-demo
    • Package name: com.example.easymockdemo
    • Packaging: Jar
    • Java: 17 or later
  3. Add the following dependencies:
    • Spring Web
    • Spring Data JPA
    • H2 Database
    • Spring Boot Starter Test
  4. Click "Generate" to download the project zip file.
  5. Extract the zip file and open the project in your IDE.

Step 2: Add EasyMock Dependencies

Add the following dependencies to your pom.xml file:

    <!-- Spring Boot Starter Web -->

    <!-- Spring Boot Starter Data JPA -->

    <!-- H2 Database -->

    <!-- Spring Boot Starter Test -->

    <!-- EasyMock -->

Step 3: Configure Application Properties

Add the following properties to src/main/resources/application.properties:


Step 4: Create the Entity Class

Create a new Java class named User in the com.example.easymockdemo package:

package com.example.easymockdemo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class User {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

Explanation: The User class is a JPA entity that represents a user in our system. It has an id, name, and email field. The @Entity annotation specifies that this class is an entity and is mapped to a database table.

Step 5: Create the Repository Interface

Create a new Java interface named UserRepository in the com.example.easymockdemo package:

package com.example.easymockdemo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

Explanation: The UserRepository interface extends JpaRepository, which provides JPA related methods for standard data access operations. This interface will be used to interact with the database.

Step 6: Create the Service Class

Create a new Java class named UserService in the com.example.easymockdemo package:

package com.example.easymockdemo;

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

import java.util.List;
import java.util.Optional;

public class UserService {

    private UserRepository userRepository;

    public List<User> findAll() {
        return userRepository.findAll();

    public Optional<User> findById(Long id) {
        return userRepository.findById(id);

    public User save(User user) {
        return userRepository.save(user);

    public void deleteById(Long id) {

Explanation: The UserService class contains methods for CRUD operations. It uses UserRepository to interact with the database. The @Service annotation indicates that this class is a service component in the Spring context.

Step 7: Create the Controller Class

Create a new Java class named UserController in the com.example.easymockdemo package:

package com.example.easymockdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class UserController {

    private UserService userService;

    public List<User> getAllUsers() {
        return userService.findAll();

    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        return userService.findById(id)

    public User createUser(@RequestBody User user) {
        return userService.save(user);

    public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) {
        return userService.findById(id)
                .map(existingUser -> {
                    return ResponseEntity.ok(userService.save(user));

    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        return userService.findById(id)
                .map(user -> {
                    return ResponseEntity.noContent().build();

Explanation: The UserController class handles HTTP requests and responses. It uses UserService to perform CRUD operations. The @RestController annotation indicates that this class is a RESTful controller. The @RequestMapping("/users") annotation maps HTTP requests to /users to the methods in this controller.

Step 8: Create the Main Application Class

Create a main application class named EasyMockDemoApplication in the com.example.easymockdemo package:

package com.example.easymockdemo;

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

public class EasyMockDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EasyMockDemoApplication.class, args);

Explanation: The EasyMockDemoApplication class contains the main method, which is the entry point of the Spring Boot application. The @SpringBootApplication annotation is a convenience annotation that adds all the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the specified package.

Step 9: Create EasyMock Test Class

Create a new test class named UserServiceTest in the src/test/java/com/example/easymockdemo package:

package com.example.easymockdemo;

import org.easymock.EasyMock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

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

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

public class UserServiceTest {

    private UserService userService;

    private UserRepository userRepository;

    public void setUp() {
        User user1 = new User();
        user1.setEmail("[email protected]");

        User user2 = new User();
        user2.setEmail("[email protected]");

        List<User> users = Arrays.asList(user1, user2);



    public void testFindAll() {
        List<User> users = userService.findAll();
        assertEquals(2, users.size());

    public void testFindById() {
        Optional<User> user = userService.findById(1L);
        assertEquals("Rajesh", user.get().getName());

    public void testSave() {
        User user = new User();
        user.setEmail("[email protected]");

        User savedUser = userService.save(user);
        assertEquals("Rajesh", savedUser.getName());

    public void testDeleteById() {

Explanation: This test class uses EasyMock to mock the UserRepository and test the UserService

The @SpringBootTest annotation tells Spring Boot to look for a main configuration class and use that to start a Spring application context. 

The @MockBean annotation tells Spring Boot to create a mock instance of UserRepository and inject it into the UserService.

Step 10: Run the Tests

Run the tests using your IDE or by executing the following command in the terminal:

mvn test

You should see output indicating that all tests have passed successfully.


In this tutorial, we demonstrated how to use EasyMock to test CRUD operations in a Spring Boot application. We created a simple Spring Boot application, added EasyMock dependencies, and wrote tests to mock the UserRepository and test the UserService

By following these steps, you can efficiently test your Spring Boot applications and ensure that your CRUD operations work as expected. This approach allows you to create mock objects and verify their interactions, making your tests more robust and reliable.

To learn more about EasyMock, check out this guide: EasyMock.