4 simple ways to stub a void method with Mockito

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is going to look at the different ways that we can stub a void method when using mokito first we're going to look at the different ways that we might stop the method when we have a spy and when we have a mock object then we're going to look at how we can throw exceptions from a void method within our mock or our spy and then lastly we're going to look at how we can use the answer functional interface to provide custom logic within our test class for when we want to achieve a slightly more customized void method within our test if you like this video please give it a thumbs up and if you want to see more weekly videos on java also subscribe to the channel [Music] so if you take a look at the class we'll be testing it's called chess player and the chess player has a string which will be their name and also a category which is also string we also store their age the number of wins and the number of points that this chess player has and then in the constructor we'll be instantiating the name and the age of our chess player so we have two void methods that we'll be testing for the purpose of this video one is called assign category so first it will check if the age is less than five and if it is we'll be throwing a runtime exception otherwise if the age is less than 18 the category that the chess player has will be under 18s otherwise it will be senior and then the second void method we have is called assign score stats and in theory we'll be connecting to a database to obtain all of the wins that they have here i'm just manually setting the wins to three and then we'll also be calculating the number of points that the chess player has achieved and here i'm manually setting it to nine so we're going to be testing these two droid methods using mokito and we're going to look at the various ways that we can start both of these methods and i'm going to begin by testing the spy version of our chess player class okay so if i were to call the chess player spies assign category method we would expect this print line to be printed and then we would expect the category to be assigned as senior because the age is going to be 30 and then i'm just going to assert what the category is so we can see that the test has passed and that is signing category for magnus has been printed and this has come from the assign category method just here so with our spy object because it represents a real object or the behavior of a real object we can nullify the assign category method to actually do nothing and we can do this by typing do nothing and defining exactly when we want to do nothing and that will be whenever the chess player spy instance is calling the assign category method so now that we know nothing is going to happen when we call a sign category if i call a sign category just down below this assertion should now fail because the assigned category is going to be null now i'm just going to copy this method over and we can do the exact same but using a mop so i'm just going to change my spy into a mock and i'm going to update some of the names now by using a mock instance as opposed to a spy instance all of the methods that we have within our chess player instance are now going to be nullified so when we call chess player mock assign category and chess player mock get category both are going to return null so this test should pass because when we call get category we're going to return null but just as we've done above with the spy instance we've kind of flipped the way that the method behaves so with the spy we want to do nothing whereas with a spy the real sort of instance application behavior is going to be executed with a mock it's all nullified but with a mock let's say we want to reverse that and we actually want to call the real method for both the assign category method and the get category method so first i'm just going to control that whenever we call the getage method on our mock that we return 30 and now when we call assign category and get category what we want our mock object to do is to actually call the real method and we can achieve this by using the do call real method from the mokito library we need to pass in the mock object that we would like to call it on and we need to specify the exact method on our mock that we would like to call the real method upon so we'll have this for assign category and then we'll also have this for get category so now if i run this test we can expect it to fail because we would expect this value to instead return senior so we can see that the test has failed just as we expect it to now makita also allows us to mock the void methods that we have to also throw exceptions so if i go back to the chess player class i'm just going to change this exception to throw a normal exception and as a result we're going to have to throw that exception from the method call i'm just going to have to make a few changes within my test class to allow this new throne exception and i'm just going to copy this test method where we're using a mock so we can now test for that exception now once again i have a chess player mock instance using the mokito mock method and we're going to call the assign category method on that mock instant now what i want to test is that this exception is actually being thrown and that we don't have a runtime exception that otherwise won't require us to handle it by throwing it or by catching it in a try catch block so what i will do for my test is expect an exception to be thrown and then when we call the assign category method i want to make sure that the exception is going to be thrown so i can use mokito to enforce the void method to now throw an exception rather than calling the real method or doing nothing and i can achieve this through the do throw method so we have do throw and then i pass in the class that i would like to be thrown which is exception then i pass in the mop and i use the assign category method so by forcing the exception class to be thrown from our mock instance of the chess player we've partially covered the behavior of our assigned category method where it's throwing an exception we would also want to test where we're passing in an age that is less than five to test the inner logic of this method however if we just want to test that an exception can be thrown from this class then we can use the do throw method with the mocked instance now the final test i'm going to write is going to use the do answer method from mokito and this will be the fourth and final way that we can stub a void method so we're going to be stubbing the assign score stats method from our chess player class now the answer functional interface will be really useful for us to represent how this database might be working within the test and we can extract it into its own instance within our test to be reused however here i'm just going to pass it within a lambda expression so we can conveniently see how to use the do answer method from the mokito library so i've defined the chess player and the chess player spy and what i want to do is call the assign score stats on our spy but just above i want to use a answer functional interface representation with a lambda expression to enforce how this assigns scorestats method is going to behave so first we want to type do answer and as we can see this accepts an instance of the answer functional interface so i'm just going to pass in a lambda where the invocation on the mock that we have i'm just going to call it chess p and now i'm going to extract an instance off the chess player by using the get mock method on the invocation and then i'm just going to set the wins to three and to set the number of points to nine so we're going to use this implementation whenever we want to call the chess player spy and then it's the assign score stats method call i'm just going to finish this test off with a couple assertions on the number of wins and points so the do answer method provides us with a lot of flexibility to how we can control void methods from a mock or from a spy i'll just leave the card to my video on the answer functional interface if the logic behind the lambda expression can be a little bit hard to follow but that concludes this video on four different ways that we can stub a void method when using a mock or a spy with mokito so first we've had a look at how we can do nothing with a void method then we've looked at how we can use the real method which is particularly useful when we're using a mocked object i've looked at how we can throw an exception from a mock or a spy object and then lastly how we can provide custom logic using do answers for when we want to control the actual behavior of a void method for our tests
Info
Channel: Aneesh Mistry
Views: 7,251
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, computer science, software engineer, Mockito, JUnit, IntelliJ, Software Engineer, Void methods, doThrow, doNothing, doAnswer, Aneesh Mistry Tutorial, Aneesh Tutorial, Java
Id: 4Vbp7rwQeLA
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Mon Dec 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.