Mockito 3 - Stubbing method with when() and thenReturn()
Aug 15, 2023
Mockito 3 - Stubbing method with when() and thenReturn()
► ►Personal queries? - Follow me on LinkedIn - / dinesh-varyani ► ►This video is part of my Complete Mastering Mockito 3 Course playlist: • Mockito 3 Tutorials ►Source Code - https://github.com/dinesh-varyani/moc … ►Click here to subscribe - https://www.youtube.com/user/hubbersp … Watch all my playlist here: ►Data Structures and Algorithms Course playlist: • Data Structures and Algorithms in Jav… ►Mastering JUnit 5 - https://www.youtube.com/playlist?list … ►Mastering Mockito 3 - • Mockito 3 Tutorials ►Analysis of Algorithms - • Analysis of Algorithms ►Linked List Data Structures - • Linked List Data Structures ►Array Data Structures - • เพลย์ลิสต์ ►Stack Data Structure - • Stack Data Structure ►Queue Data Structure - • Queue Data Structure ►Binary Tree Data Structure - • Binary Tree Data Structure ►Graph Data Structure - • Graph Data Structure ►Binary Heap Data Structure - • Binary Heap Data Structure ►Trie Data Structure - • Trie Data Structure ►Dynamic Programming Algorithms - • Dynamic Programming Algorithms ►Hashing Data Structures - • Hashing Data Structures ►Sorting and Searching - • Sorting and Searching ►String Algorithms - • String Algorithms ►Java Programming Tutorial - • Java Programming Tutorial ►Design Patterns in Java - • Design Patterns in Java Follow Me On Social Media ►Website - http://www.hubberspot.com ►Facebook - / dinesh.varyani ►Instagram - / Get complete free course on Data Structures and Algorithms at - • Data Structures and Algorithms in Jav… . Subscribe to the channel for all free courses at - https://www.youtube.com/user/hubbersp … 🙏 Thank you for your continuous love and support. I humbly request you to help this channel grow more, for that please Like, Share and Subscribe to my channel. Your support will motivate me to add more valuable content. || LIKE || SHARE || SUBSCRIBE || For a free complete course download our android app on Mockito 3 Tutorials - https://play.google.com/store/apps/de … COMPLETE SOURCE CODE - https://github.com/dinesh-varyani/moc … Visit my blog for more such free videos - http://www.hubberspot.com
Content
3.54 -> Hello, everyone. So, in this section of
course, we will be seeing the demonstration
9.75 -> of how we can perform a basic stubbing in
mockito. We'll see what's the advantage of
15.06 -> it. So here, in our main Java folder, I will
creating a package will provide a package of
26.13 -> stubbing. And friends in our previous
lecture, we saw the annotation support, where
36 -> we used the book class, book repository, and
book service, which was our class under test.
45.45 -> So I'll simply copy these three stuff. And
we'll reuse the code in stubbing.
63.06 -> So, friend, let's in our book service, we
have written one method, which you want to
66.45 -> test, let's have a give name to test. Public,
we provide the return type is int. And let's
76.2 -> say the name of the method is calculate total
cost. So for an In this method, what we are
88.29 -> doing is, let's say we are given a list of
book IDs. So what we'll do here is that we
101.37 -> are given a list of book IDs, we will go to
the book repository, we'll get all the books
108.15 -> associated with this book IDs. After we get
the list of books, we will iterate over each
115.11 -> book one by one, we'll get the price of the
book. And we'll simply do the sum of it. And
121.11 -> finally, we'll return the total cost of the
books. We'll provide a for loop will iterate
138.21 -> over book IDs one by one. And here we will
call book repository dot find book by book
152.52 -> ID. So your book repository in a real project
will go to database and bring the book by
161.97 -> book ID. But here in order to test calculate
total cost method, we'll simply stop this
170.97 -> stuff. And we'll return a pre configured
response. So this will return us the book
179.31 -> from the repository. So for software as you
want to get the total cost of the books,
185.55 -> here, what we'll do, we'll simply create an
integer variable total. we'll initialize it
193.62 -> with zero. And as soon as we have performed
the fetching of the book, what we'll do,
201.33 -> we'll simply do total total plus book dot get
price. And after iterating over each and
214.71 -> every book and getting its price and adding
it to total. At last, we'll simply return
222.48 -> total. So friends, we want to test this
method, calculate total cost. And that's fine
232.62 -> book by book that is not present. So we'll
add this method into the book repository
238.32 -> interface. So for now, let's go to our test
folder. Here, I will simply create the
245.79 -> package we'll give it a name and stubbing and
here I will create the test class. Let's say
259.08 -> give book
261.54 -> service test because your book services our
class under test. And also friend will use
270.09 -> mockito annotations. So what we'll do,
274.62 -> we'll simply extend it with mockito.
Extension so that we can use the annotations
288.81 -> in this test class. So then as we have booked
services class under test
295.44 -> we'll simply call inject mocks And we'll tell
mockito Network Services our class under test
307.65 -> using added inject mocks integration. And as
book services when external dependency book
314.28 -> repository, we will inject it using at the
red mock.
329.67 -> So here we'll create our test method. Let's
review the name of the test is Test, test
346.68 -> calculate total cost of books. So friends
here, one important thing to notice, when we
357.06 -> are testing book service,
361.529 -> calculate total cost method, we have to pass
the list of book IDs. When the execution
370.739 -> point will reach here, it will see that book
repository find book by book ID method is
377.639 -> getting called. So from here, you can see
that we are passing here a book ID. So when
384.809 -> we will do stubbing of book repository dot
find book by book ID. And we'll pass the book
390.899 -> ID. What mockito will do is mockito will use
the equals method while matching these
398.189 -> arguments. So what are the arguments here
passed to our calculate total cost method.
405.029 -> And whatever we have provided stub, in book
service test class, which we will be seeing
409.409 -> in some time, it should match. So if we
haven't provided stubbing, then the default
416.129 -> value will be returned by the mockito. So
here, first, we'll create the list of book
424.529 -> IDs, because we need to pass it to
430.11 -> calculate total cost method. So book IDs,
let's say we add a string as 1234 as book ID
454.83 -> one,
459.779 -> and 1235. So let's say want to calculate
total cost of these two books. So we'll call
471.809 -> book service. dot calculate total cost, and
we'll pass book IDs. And then it will return
486.149 -> us the total cost. So this would be our
actual cost.
497.04 -> So yeah, well, we'll run this test case, this
method will be called with the book IDs which
504.27 -> we have created here. So the execution point,
when it will reach here, it will see that we
512.07 -> are calling book repository dot find book by
book ID and we are passing a single book ID.
518.76 -> But as we know, that book repository
currently is only interface, and its
524.85 -> implementation is yet to be implemented. So
this is the beauty of stubbing that we don't
531.15 -> have actual implementation of of our external
dependency. But still, we can test our book
536.64 -> service. So what we can do is we can tell
mockito, what to do, when this method is
541.89 -> called with a specific book ID. So here, we
can perform stubbing, using when and then
554.4 -> return. So in when we simply provide book
repository, dot, find book by book ID. And
565.59 -> let's say we provide 1234 because 1234 will
be our first book ID. So here we'll import
574.17 -> one from all got mockito. So it's a static
method. So this one is used in combination
583.53 -> with den return. And here, if you see the
return type of find book by book ID, it's
592.89 -> actually a book. So here when we'll do den
return, so here it expects a book Because as
602.94 -> soon as we have passed a method in mockito
dot when, when we'll call them return it
609.87 -> knows that what find book by book it will
return. So then return asked us that what do
615.84 -> you want to return. So let's say we want to
return a book. So what we need to do here is
624.24 -> if you go to our annotation support class,
here, we have created two books, let's add,
630.24 -> simply copy this and let's paste it here.
Friends, here, we have created two books.
642.99 -> Now, why we have created these two books is
that we don't want to go to database using
648.66 -> book repository. Because Currently, there is
no implementation of book repository. So,
656.13 -> when we do stubbing, we need to tell mockito
that when we are calling find book by book ID
660.87 -> method, by passing in a book ID then return a
specific book to us, because our actual
668.91 -> testing is to calculate the total cost
because it is a very less important that what
676.11 -> book is returned by this book repository,
find book by book ID method, because whatever
682.35 -> we pass here, we need to test this stuff,
that whatever the price is, it is getting out
688.53 -> from this book, when we are performing actual
total, it should match to our expectation.
696.27 -> So, here, we are telling mockito that SEO
mock does dependency when this method will be
702.84 -> called with 1234 as book ID, then return book
one. So friendly, you can see that in our
714.54 -> test case, we are telling mockito that when
book repository dot find book by book it is
720.6 -> called and we are passing in an argument here
as 1234. So mockito will keep a record of it
730.44 -> and when the actual method will be called
here. So it will check that okay, book
735.48 -> repository, find book by book ID method is
getting called with a specific book ID. So it
742.71 -> will actually use the equals method here to
compare this book ID with this book ID and if
750.09 -> it matches, then it will simply return book
one for us. So from here, you can see that
757.02 -> inside this for loop, we are passing into
book IDs, so book repository will be called
762.21 -> twice. So we need to tell mockito that when
book repository dot find book by book ID
772.89 -> method is called with book IDs 1235 then
return book two. So this is the book to
782.07 -> having book ideas 1235 So friends, you can
see the price of two books is 504 100. And
790.98 -> then we'll do the sum of it we get 900 So
here, if I provide a third equals
802.5 -> let's our expected value should be 900. And
the actual value is actual cost. we'll import
811.77 -> our third equals. So friends, if I run the
test case, so friends, you can see the desk
826.62 -> it was executed successfully.
829.8 -> And mockito did return book one and Book Two.
Hear when it found that book IDs 1234 and
840.72 -> 1235. Because we told Marketo what to do,
when these specific methods are called with
848.43 -> these arguments. So marketer use the equals
method to match 12341235 when actually
857.49 -> executed our method in class under test so
for an In this lecture, we saw the first way
867.51 -> to perform the stubbing in mockito. Our next
lecture we'll see another way to do the
875.13 -> stubbing. So friends, I hope you like this
video. Thanks Have a nice day.
Source: https://www.youtube.com/watch?v=6RQ37DyCizo