Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Test Doubles and Seams in Unit Testing: A Practical Implementation with Java, Assignments of Software Engineering

This assignment demonstrates the use of test doubles and seams in unit testing using a java example. It explains the concept of test doubles, their importance in achieving consistent and reliable tests, and explores different types of seams. The assignment provides a practical implementation of inheritance seam with dependency injection, showcasing its benefits in terms of modularity, testability, and flexibility. It includes source code for the reservationservice, rankingservice, fakerankingservice, and customer classes, along with junit tests to verify the behavior of reservationservice with both real and fake implementations of rankingservice.

Typology: Assignments

2024/2025

Uploaded on 03/18/2025

amlan-chowdhury
amlan-chowdhury 🇺🇸

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Module 4: Assignment 1
Amlan Chowdhury (G01465085)
February 23, 2025
In this assignment, I implemented the ReservationService example to
demonstrate the use of test doubles and seams in unit testing. The ob-
jective was to isolate the behavior of the ReservationService class from its
dependency on the RankingService, which provides customer rankings.
The RankingService produces different outputs on each call due to its
random behavior. To achieve consistent and reliable tests, I used a test
double called FakeRankingService that always returns the same rank. This
allowed me to control the inputs and predict the outputs, ensuring consistent
test results.
Understanding Test Doubles
A test double is an object used in testing to mimic the behavior of a real
component. It replaces the actual dependency in order to provide controlled
and consistent behavior. In this project, I used a fake object called FakeR-
ankingService, which always returns a fixed rank. This ensured predictable
and consistent behavior during testing.
Why Use Test Doubles?
The real RankingService generates random ranks for each customer, making
it difficult to write reliable tests. To address this, I created FakeRankingSer-
vice as a test double. By returning a consistent rank, it allowed me to focus
on testing the logic of ReservationService without worrying about random-
ness. This approach ensured that the tests were repeatable and reliable.
1
pf3
pf4
pf5

Partial preview of the text

Download Test Doubles and Seams in Unit Testing: A Practical Implementation with Java and more Assignments Software Engineering in PDF only on Docsity!

Module 4: Assignment 1

Amlan Chowdhury (G01465085)

February 23, 2025

In this assignment, I implemented the ReservationService example to

demonstrate the use of test doubles and seams in unit testing. The ob-

jective was to isolate the behavior of the ReservationService class from its

dependency on the RankingService, which provides customer rankings.

The RankingService produces different outputs on each call due to its

random behavior. To achieve consistent and reliable tests, I used a test

double called FakeRankingService that always returns the same rank. This

allowed me to control the inputs and predict the outputs, ensuring consistent

test results.

Understanding Test Doubles

A test double is an object used in testing to mimic the behavior of a real

component. It replaces the actual dependency in order to provide controlled

and consistent behavior. In this project, I used a fake object called FakeR-

ankingService, which always returns a fixed rank. This ensured predictable

and consistent behavior during testing.

Why Use Test Doubles?

The real RankingService generates random ranks for each customer, making

it difficult to write reliable tests. To address this, I created FakeRankingSer-

vice as a test double. By returning a consistent rank, it allowed me to focus

on testing the logic of ReservationService without worrying about random-

ness. This approach ensured that the tests were repeatable and reliable.

What Are Seams?

A seam is a point in the code where behavior can be changed without modi-

fying the code itself. It allows you to substitute a test double for a real object

in unit tests. I considered three types of seams:

  • Compiler Seam
  • Inheritance Seam without Dependency Injection
  • Inheritance Seam with Dependency Injection

Choosing the Seam: Inheritance Seam with

Dependency Injection

I chose Inheritance Seam with Dependency Injection for the following reasons:

  • It allows me to pass the RankingService as a constructor argument,

making it easy to swap the real service with FakeRankingService during

testing.

  • It maintains the public API of ReservationService without requiring

any modifications.

  • It adheres to the Dependency Inversion Principle, enhancing the mod-

ularity and testability of the design.

This approach allowed me to inject FakeRankingService during testing

while using RealRankingService in production. It kept the design clean and

flexible, making it easier to maintain and extend.

Source Code and Test Code

The implementation consists of the following components:

  • ReservationService: This class contains the main logic for making

reservations based on customer rank.

  • RankingService: An interface that defines the contract for getting a

customer’s rank.

  • RealRankingService: The real implementation of RankingService

that generates a random rank.

1 p u b l i c c l a s s FakeRankingService implements R a n k i n g S e r v i c e { 2 @Override 3 p u b l i c i n t getRank ( Customer customer ) { 4 r e t u r n 7 5 ; 5 } 6 }

  1. Customer.java

Description: This class represents a customer making a reservation. It

stores the customer’s name.

1 p u b l i c c l a s s Customer { 2 p r i v a t e S t r i n g name ; 3 4 p u b l i c Customer ( S t r i n g name ) { 5 t h i s. name = name ; 6 } 7 8 p u b l i c S t r i n g getName ( ) { 9 r e t u r n name ; 10 } 11 }

  1. ReservationService.java

Description: This is the class under test. It contains the main logic for

making reservations based on customer rank.

1 p u b l i c c l a s s R e s e r v a t i o n S e r v i c e { 2 p r i v a t e R a n k i n g S e r v i c e r a n k i n g S e r v i c e ; 3 4 p u b l i c R e s e r v a t i o n S e r v i c e ( ) { 5 t h i s. r a n k i n g S e r v i c e = new R e a l R a n k i n g S e r v i c e ( ) ; 6 } 7 8 p u b l i c R e s e r v a t i o n S e r v i c e ( R a n k i n g S e r v i c e r a n k i n g S e r v i c e ) { 9 t h i s. r a n k i n g S e r v i c e = r a n k i n g S e r v i c e ; 10 } 11 12 p u b l i c v o i d r e s e r v e ( Customer customer ) { 13 i n t customerRank = r a n k i n g S e r v i c e. getRank ( customer ) ; 14 System. out. p r i n t l n ( ” Customer Rank : ” + customerRank ) ; 15 i f ( customerRank > 5 0 ) { 16 System. out. p r i n t l n ( ” R e s e r v a t i o n c o n f i r m e d f o r VIP customer : ” + customer. getName ( ) ) ; 17 } e l s e {

18 System. out. p r i n t l n ( ” R e s e r v a t i o n c o n f i r m e d f o r r e g u l a r customer : ” + customer. getName ( ) ) ; 19 } 20 } 21 }

Test Code

ReservationServiceTest.java

Description: This JUnit test verifies the behavior of ReservationService

with both RealRankingService and FakeRankingService.

1 import org. j u n i t. j u p i t e r. a p i. Test ; 2 import s t a t i c org. j u n i t. j u p i t e r. a p i. A s s e r t i o n s. ∗ ; 3 4 p u b l i c c l a s s R e s e r v a t i o n S e r v i c e T e s t { 5 6 @Test 7 p u b l i c v o i d t e s t R e s e r v e W i t h F a k e R a n k i n g S e r v i c e ( ) { 8 R a n k i n g S e r v i c e f a k e R a n k i n g S e r v i c e = new FakeRankingService ( ) ; 9 R e s e r v a t i o n S e r v i c e r e s e r v a t i o n S e r v i c e = new R e s e r v a t i o n S e r v i c e ( f a k e R a n k i n g S e r v i c e ) ; 10 11 Customer customer = new Customer ( ” John Doe ” ) ; 12 r e s e r v a t i o n S e r v i c e. r e s e r v e ( customer ) ; 13 } 14 }

Test Results

The following test results were obtained using Maven:

Customer Rank: 75

Reservation confirmed for VIP customer: John Doe

Customer Rank: 2

Reservation confirmed for regular customer: Jane Doe

Results:

  • Tests run: 2
  • Failures: 0
  • Errors: 0