Sunday, January 11, 2026
Cart / 0,00 EGP

No products in the cart.

  • Login
  • Register
BAWABAA.COM
No Result
View All Result
  • Home
  • Archives
    • Pages
    • Posts
    • Glossary
    • Products
    • Websites
    • Portfolios
    • Services
    • Solutions
    • Peoples
    • Movies
  • Taxonomies
  • Contact
  • Home
  • Archives
    • Pages
    • Posts
    • Glossary
    • Products
    • Websites
    • Portfolios
    • Services
    • Solutions
    • Peoples
    • Movies
  • Taxonomies
  • Contact
No Result
View All Result
BAWABAA.COM

Home » Posts » Unit Testing

Unit Testing

by MOUSTAFA
December 15, 2025
in Uncategorized

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

RelatedPosts

WordPress Conceptual System Model

List of Curated Youtube Channels

WordPress Development Services

WordPress Full Courses on YouTube

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");
    }
}
Share9Tweet6Share2
Previous Post

Unit testing – Stub Object in Java

Next Post

Soft skills Outlines

MOUSTAFA

MOUSTAFA

Technology, Engineering and Business Analyst

Related Posts

Uncategorized

WordPress Conceptual System Model

Uncategorized

List of Curated Youtube Channels

Free bold abstract painting background
Uncategorized

WordPress Development Services

Abstract colorful mesh hanging Oxford
Uncategorized

WordPress Full Courses on YouTube

Abstract wavy texture black background
Uncategorized

The “Green Padlock” on Localhost Cheat Sheet

worms eye view of spiral stained glass decors through the roof
Uncategorized

Comprehensive List of eCommerce Tools by Type and Business Size

Next Post
black and yellow round lantern

Complete Guide to Software Development Methodologies

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • WordPress Conceptual System Model
  • List of Curated Youtube Channels
  • WordPress Development Services
  • WordPress Full Courses on YouTube
  • The “Green Padlock” on Localhost Cheat Sheet

Recent Comments

No comments to show.

Archives

  • January 2026
  • December 2025

Categories

  • Uncategorized
  • Home
  • Archives
  • Taxonomies
  • Contact

MOUSTAFA ALSAYEH

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
  • Login
  • Sign Up
  • Cart
No Result
View All Result
  • Home
  • Archives
    • Pages
    • Posts
    • Glossary
    • Products
    • Websites
    • Portfolios
    • Services
    • Solutions
    • Peoples
    • Movies
  • Taxonomies
  • Contact

MOUSTAFA ALSAYEH