



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!
What Are Seams?
Choosing the Seam: Inheritance Seam with
Dependency Injection
Source Code and Test Code
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 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 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
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