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

Unit Testing

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

Links:https://courses.marmicode.io/

Test Doubles: Dummy, Stub and Mock Examples

// Library.java
public class Library {
    private final LibraryRepository libraryRepository;
    private final NotificationService notificationService;
    private final LoggerService loggerService;

    public Library(LibraryRepository libraryRepository, NotificationService notificationService, LoggerService loggerService) {
        this.libraryRepository = libraryRepository;
        this.notificationService = notificationService;
        this.loggerService = loggerService;
    }

    public boolean addBook(String bookId, String title) {
        if (libraryRepository.bookExists(bookId)) {
            loggerService.log("Book already exists: " + bookId);
            return false;
        }
        libraryRepository.addBook(bookId, title);
        loggerService.log("Book added: " + title);
        notificationService.notifyAddBook(title);
        return true;
    }
}

// LibraryRepository.java
public class LibraryRepository {
    private final Database database;

    public LibraryRepository(Database database) {
        this.database = database;
    }

    public boolean bookExists(String bookId) {
        return database.contains(bookId);
    }

    public void addBook(String bookId, String title) {
        database.save(bookId, title);
    }
}

// NotificationService.java
public class NotificationService {
    private final EmailService emailService;
    private final SMSService smsService;

    public NotificationService(EmailService emailService, SMSService smsService) {
        this.emailService = emailService;
        this.smsService = smsService;
    }

    public void notifyAddBook(String title) {
        emailService.sendEmail("A new book has been added: " + title);
        smsService.sendSMS("A new book has been added: " + title);
    }
}

// LoggerService.java
public class LoggerService {
    public void log(String message) {
        System.out.println(message);
    }
}

// Database.java
public class Database {
    public boolean contains(String key) {
        // Dummy implementation
        return false;
    }

    public void save(String key, String value) {
        // Dummy implementation
    }
}

// EmailService.java
public class EmailService {
    public void sendEmail(String message) {
        // Dummy implementation
    }
}

// SMSService.java
public class SMSService {
    public void sendSMS(String message) {
        // Dummy implementation
    }
}

// LibraryTest.java
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

public class LibraryTest {

    @Test
    public void testAddBookWithDummy() {
        Database dummyDatabase = new Database();
        LibraryRepository libraryRepository = new LibraryRepository(dummyDatabase);
        EmailService dummyEmailService = new EmailService();
        SMSService dummySmsService = new SMSService();
        NotificationService notificationService = new NotificationService(dummyEmailService, dummySmsService);
        LoggerService dummyLoggerService = new LoggerService();

        Library library = new Library(libraryRepository, notificationService, dummyLoggerService);
        boolean result = library.addBook("1", "Dummy Book");

        // Assertion to verify the result
        assertTrue(result, "The book should be added successfully since the dummy database does not contain it.");
    }

    @Test
    public void testAddBookWithStub() {
        Database stubDatabase = mock(Database.class);
        when(stubDatabase.contains("1")).thenReturn(false);
        LibraryRepository libraryRepository = new LibraryRepository(stubDatabase);

        NotificationService notificationService = mock(NotificationService.class);
        LoggerService loggerService = mock(LoggerService.class);

        Library library = new Library(libraryRepository, notificationService, loggerService);
        boolean result = library.addBook("1", "Stub Book");

        assertTrue(result);
        verify(loggerService).log("Book added: Stub Book");
        verify(notificationService).notifyAddBook("Stub Book");
    }

    @Test
    public void testAddBookWithMock() {
        LibraryRepository mockRepository = mock(LibraryRepository.class);
        NotificationService mockNotificationService = mock(NotificationService.class);
        LoggerService mockLoggerService = mock(LoggerService.class);

        when(mockRepository.bookExists("1")).thenReturn(false);

        Library library = new Library(mockRepository, mockNotificationService, mockLoggerService);
        boolean result = library.addBook("1", "Mock Book");

        assertTrue(result);
        verify(mockRepository).addBook("1", "Mock Book");
        verify(mockLoggerService).log("Book added: Mock Book");
        verify(mockNotificationService).notifyAddBook("Mock Book");
    }
}

Donation

Buy author a coffee

Donate
Share9Tweet6Share2
Previous Post

Unit testing – Stub Object in Java

Next Post

Soft skills Outlines

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?