How to use Mockito Verify - JUnit Mockito Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video you will understand when you would want to use mokito verify to increase your unit test coverage the benefits to using mokito verify and how you would go about writing the unit tests themselves so if we take a look at the class that will be testing it's called train barriers and the trained barriers class is essentially acting as the entry to a train station and it has just a single method called passenger entry that it takes in the passenger id so this would be like swiping your card to ensure train station there are two services or co-operators to this class one is called passenger repository and the other is called the email service so when this passenger entry method is called the method will first call the register passenger on train method from the repository and if we go to the passenger repository we can see that all this call will do is update a database to include the passenger id onto that train and then the second method call would be a notify passenger method call for the email service and that would send essentially send an email confirmation to that passenger to say that they've entered this train station or enter this train so the current challenge that we face is from this passenger entry method which is known as a managing method or a manager so it doesn't actually do any work other than calling the two methods below the second challenge is also that the passenger entry method doesn't actually return anything so it returns void so there's no outcome to calling this method which we can actually assert upon so what we're trying to achieve with our tests is to actually test the indirect outputs of the method and those indirect outputs are these two method calls that are made to the individual services so i'm going to test this passenger entry method within the train barriers test class that i've created within the test java directory the test method will utilize one of the properties of a mocked class that enables us to assert upon the method calls that it makes so we can therefore use our mocked classes and test upon the method calls that have been used from it so the first thing i'm going to do is create the mocked version of our passenger repository and the email service and then i'm going to pass that into a real version of our train barriers class instant so we've created our mocked version of the passenger repository class and then the same for the email service and then we created a train barriers instance using both of those mod classes using the constructor what i can then do is call the passenger entry method on the chain barriers instance passing in just a random number for the passenger so now that we've called this passenger entry method passing in the value of three which is the passenger id we can expect our passenger entry method to call the register passenger on train method and the notify passenger method passing in that same passenger id each time so this is where we would use the mokito verify assertion which is where we can test upon our mod classes to ensure that those exact same method calls have been made so firstly i will use the verify method and with the verify method we would first pass in the mod class that we will be testing so the first one is the passenger repository mark and then we would be specifying the exact method call that we would like to verify so we would expect it to call the register passenger on train method passing in that exact same passenger id and then the same would go for the email service mock where we expected to call the notified passenger passing in that exact same passenger id we can see that this test has passed if i were to change one of these parameters say to the number two we would expect that test to now fail because it's actually calling the passenger id value of three [Music] this way if we were to change the the passenger entry method perhaps to no longer call that email service this test would also fail once more so wanted but not invoked email service notify passenger three and that's because it's no longer within that method so that summarizes this video on mokito verify when you would want to use it and also how you can use it i personally found makita verify quite difficult to get my head around as to understanding when i would want to use it but this video by no means test mojito verified to the full extent of what it can achieve there are many other types of variables and method calls that we can make with makita verify for more complex testing and i will cover this in a future video
Info
Channel: Aneesh Mistry
Views: 15,785
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, JUnit, Java, IntelliJ, Mockito, Verify, Tutorial, Aneesh Mistry Tutorial, Aneesh Tutorial, Aneesh Mistry, Java 8, Software Tutorial, Software Engineer, Mockito Tutorial, Junit Tutorial, Backend Engineer, How to test software, Test Automation, testing software, beginner software engineer, learn to code, learning to code, step by step tutorial, Mockito Verify, Software Testing, JUnit Tutorial
Id: lbN2nfsC7C4
Channel Id: undefined
Length: 5min 41sec (341 seconds)
Published: Mon May 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.