Wednesday, March 11, 2026
Cart / 0 $

No products in the cart.

  • Login
  • Register
Bawabaa Digital Solutions
No Result
View All Result
  • Home
  • Products
    • Unlock Premium Access
    • Subscriptions
  • About
    • Terms and conditions
    • Privacy Policy
    • Refund Policy
  • Contact
  • Posts
  • Home
  • Products
    • Unlock Premium Access
    • Subscriptions
  • About
    • Terms and conditions
    • Privacy Policy
    • Refund Policy
  • Contact
  • Posts
No Result
View All Result
Bawabaa Digital Solutions
Home Main

Stub Object in Unit testing with Java

Published on: February 9, 2026
Updated on: February 11, 2026

In unit testing with Java, a stub object is a simplified implementation of a class or an interface that is used to isolate the behavior of the code being tested. Stubs are commonly used to simulate the behavior of external dependencies or components, allowing you to focus on testing specific units of code without worrying about external factors.

Key Characteristics of a Stub:

  1. Simplified Behavior: Stubs often provide hardcoded responses to method calls.
  2. No Real Logic: Unlike mocks, stubs typically don’t involve intricate behavior or logic.
  3. Passive Object: They are used to supply inputs or simulate the state required for testing.

When to Use Stubs:

  • When you need to isolate the code under test from external dependencies (like databases, APIs, or other services).
  • When testing behavior that relies on specific return values or conditions from other components.

Example: Stub Implementation in Java

Here is an example of how to use a stub object in unit testing:

Scenario:

Suppose you have a UserService class that relies on a UserRepository interface to fetch user details.

// Interface
public interface UserRepository {
    User findUserById(String id);
}

// Implementation of Service
public class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public String getUserName(String id) {
        User user = userRepository.findUserById(id);
        return user != null ? user.getName() : "User not found";
    }
}

// Model Class
public class User {
    private final String id;
    private final String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Stub for UserRepository:

You can create a stub for UserRepository in your test class.

// Stub Implementation
public class UserRepositoryStub implements UserRepository {
    @Override
    public User findUserById(String id) {
        if ("123".equals(id)) {
            return new User("123", "Alice");
        }
        return null;
    }
}

Test Case Using Stub:

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

public class UserServiceTest {
    @Test
    public void testGetUserName() {
        // Arrange: Use the stub
        UserRepository userRepositoryStub = new UserRepositoryStub();
        UserService userService = new UserService(userRepositoryStub);

        // Act: Call the method
        String userName = userService.getUserName("123");

        // Assert: Verify the result
        assertEquals("Alice", userName);

        // Test for a non-existent user
        assertEquals("User not found", userService.getUserName("999"));
    }
}

Advantages of Using Stubs:

  1. Simplifies Testing: Focuses only on the unit under test by removing external complexities.
  2. Improves Speed: Avoids delays caused by real components (e.g., database or network calls).
  3. Reliability: Provides predictable outputs for test cases.

Considerations:

  • Stubs are less flexible compared to mocks because they provide static responses.
  • For more dynamic and complex behavior testing, you might use a mocking framework like Mockito.

Would you like an example using Mockito for dynamic stubbing?

Donation

Buy author a coffee

Donate
Share9Tweet6Share2
Previous Post

A curated collection of YouTube playlists to help learn WordPress.

Next Post

Unit testing – Stub Object in Java

MOUSTAFA

MOUSTAFA

Technology, Engineering and Business Analyst

Categories

  • Main (78)

Recent Posts

  • WordPress Main Subjects – Complete Overview of Core Topics
  • How to Build a Multilingual WordPress Website: Methods, Pros, and Cons
  • Website / Project Development Hierarchy
  • WordPress Hooks Explained: Actions and Filters for Beginners
  • Software Adaptation Maturity Model SAMM
  • What is Screaming Architecture?
  • WordPress Deployment on AWS EC2 (Red Hat / Amazon Linux)
  • eCommerce Platforms Directory: List of Providers by Domain
  • Understanding Modules, Packages, Components, Libraries, Frameworks and Dependencies in Software Design
  • List of Curated YouTube Playlists
  • WordPress Conceptual System Model
  • Computer Science Courses on YouTube
  • Web Hosting Platforms Directory: List of Providers by Domain
  • Computation Theory
  • Software Building Blocks: A Modern Dev Guide
  • Software Architecture and Design
  • List Of Curated Websites
  • WordPress Development Services
  • WordPress Full Courses on YouTube
  • The “Green Padlock” on Localhost Cheat Sheet

HTML Content Title

  • Home
  • Products
  • About
  • Contact
  • Posts
WhatsApp: +201111128344

Bawabaa.com

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Sign Up with Google
OR

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In

You cannot copy content of this page

  • Login
  • Sign Up
  • Cart
No Result
View All Result
  • Home
  • Products
    • Unlock Premium Access
    • Subscriptions
  • About
    • Terms and conditions
    • Privacy Policy
    • Refund Policy
  • Contact
  • Posts
SAVED POSTS

Bawabaa.com

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?