Mockito 3 - What is Stubbing ?
Aug 15, 2023
Mockito 3 - What is Stubbing ?
► ►Personal queries? - Follow me on LinkedIn - https://www.linkedin.com/in/dinesh-va … ► ►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 - https://www.facebook.com/dinesh.varyani/ ►Instagram - https://www.instagram.com/dinu.varyani/ 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.659 -> Hello everyone. So, friends in this section
of course, we will be discussing about how we
9.179 -> can do stubbing in mockito So, in this
lesson, we'll see that what is stubbing and
15.899 -> why it is important to do in our unit test.
So, here So, if you see, one of the primary
26.729 -> benefit of mockito is the ability to return a
provided response when a specific method is
32.519 -> called on mock dependency. So, here what it
means that as we're mocking out the external
38.909 -> dependency, whenever class under test will
call any method on mock dependency, then we
45.989 -> need to tell mockito what to do. And this is
also one of the primary benefit of mockito
51.839 -> that we can tell mockito that if any specific
method is called on mock dependency, then
57.869 -> simply return a provided response or we can
say a configured response by a developer. So,
64.709 -> that other parts of the code can be tested of
the class under test. So, friends this
72.389 -> process of writing how a given mock method
should be is called stubbing. And also
78.539 -> friends in our previous lecture, we saw a
example, when we used when and then return
83.729 -> methods. So, those methods did the stubbing
for us, but, in this section of course, we
90.689 -> will be discussing stubbing in greater detail
and we will see the different methods mockito
96.239 -> provides to perform the stubbing in our
mocks. So, friends at a higher level, this
106.469 -> feature is usually performed in two ways, the
first ways using mockito static method when
115.109 -> along with then return. So, in a previous
lecture, we also saw a simple example, where
121.769 -> we use when and then return. So, this is a
combination of method. So, you have this
127.979 -> combination states that when any specific
method is called on mock object then return a
133.829 -> pre configured value or response. So, friends
here mockito provided the names such as when
140.279 -> and then return because, when we use these
methods in our tests, it makes readability of
146.309 -> our tests clear and we simply read it as that
when any specific method on the mock object
153.569 -> is called then return a pre configured
response. So, friend this is the first way
162.569 -> The second way is using mockito static method
do return plus when. So, this approach also
170.249 -> provides a good readability it states that do
return a pre configured value when specific
176.969 -> method is called on mock object. So, friends
yes chaining of the operation which we are
181.709 -> seeing here does the stubbing for us. In our
upcoming lectures, we will see the
187.349 -> demonstration of both of these approaches.
Moving ahead. So, when widely performed
193.949 -> stubbing in mockito, there are few tips
around it. makedo uses equals method while
202.319 -> matching arguments during stubbing. So, for
if we go back here, if you see, when we call
210.029 -> when method, we actually provide the mock
dependency, and we call this specific method
216.479 -> on the Mount dependency. So, when we call the
method, it may take certain arguments. So,
223.709 -> what mockito does is, whatever arguments you
provide in when method when we execute our
229.379 -> test, and whenever call reaches to our mock
dependency, where we are calling the specific
236.549 -> method there, it simply uses the equals
method to check whatever we have stopped is
242.789 -> correct or not. So, for an in case you don't
get this tip, in our next lecture, we'll see
248.789 -> the demonstration around it. The other tip
is, let's suppose when we are doing the unit
258.509 -> testing of our class under test, which is
calling the external mock dependency, so when
264.839 -> the point of execution reaches where the call
goes to the mock dependency, and if you're
270.419 -> not stubbed, that particular method, then
mockito simply returns a default value. So
278.249 -> here if you see we are provided when and then
return. So and then return we simply provide
285.089 -> a pre configured value. And if we haven't
done this stubbing, what mockito does is it
292.349 -> simply return the default values. So let's
say if the return type is of primitive, so
298.349 -> zero will be returned.
301.05 -> If it is a Boolean then false is returned. If
it is an object or an array, then null is
307.74 -> returned.
310.679 -> And if it is a collection then empty
collection is returned. So friends in our
315.659 -> next lecture we'll see the demonstration of
all those stuffs which are discussed here. So
323.099 -> let's go to intelligence see that how we can
perform basic stubbing in mockito. First
328.739 -> we'll see when then return methods and then
we'll see do return when methods. So friends,
334.949 -> I hope you liked this video. Thanks. Have a
nice day.
Source: https://www.youtube.com/watch?v=OWP3bQIMHY8