How to use @InjectMocks with Mockito - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to take a look at when we might want to use the inject mox annotation and some of the benefits that it brings to us when testing our individual classes especially when the class depends upon other services within its own methods so if we take a look at the class that we'll be testing it's called customer order and this customer order would be taking place within say a restaurant and that customer order can be a vegetarian breakfast a meat breakfast a vegetarian dinner or a meat dinner and when they call one of these methods they will use one of these two services to obtain the menu that each one represents so we have this breakfast waiter class and that will be used for the vegetarian breakfast and the meat breakfast and we can see that they have two methods so get meat menu which will return apple sausages bacon or toast then we have a similar one for get vegetarian menu which will just have vegetarian items and then we have the vegetarian dinner and the meat dinner methods below and they will use the dinner waiter to get the vegetarian menu or the meat menu which has kind of a similar sort of list of strings that are returned but for the dinner so if we move back to the customer order class we can see that the breakfast waiter and the dinner waiter haven't actually been instantiated and there are three key ways that we can instantiate these two services so the first could be through the constructor like this the second way could be through individual setters just like this below and the final way could be through auto wiring these classes if we're using an inversion of control container but i'm going to leave both of these services instantiated through a constructor so i'm going to move over to our test class and i've already written out a couple tests where we would like to test for the vegetarian breakfast and also for the meat dinner so what i have here is i've created the expected breakfast just as a simple and arraylist i've then defined the actual breakfast as being as coming from the customer order and using the vegetarian breakfast method and that will call the get vegetarian menu from the breakfast waiter so if i come back to our test and try to run it so we can see we we have a null pointer exception caused by line 24 so this is where we're calling the vegetarian breakfast and that's because the breakfast waiter hasn't been instantiated with our customer order so one way that we can do this is by adding a line to the start of the method which will instantiate the breakfast waiter and then when we rerun the test we can expect it to pass however as i mentioned before there are many different ways that the services can be instantiated within the class so sometimes they might be auto-wired or they might be set through a setter and an individual class might have many different services so this customer order might not just have two services but it might have six or seven or eight different services that it depends upon so our question here is how do we inject those services efficiently into our customer order without concerning ourselves with how they're actually instantiated within the class implementation itself so do we always have to instantiate this customer order within the test class using such as a before method or is there a better way of doing this and that's where makita can come in to help us so for this video i'm going to be using annotation based for mokito and the first thing i want to do is create the mocks and the spies for our custom order that they will be using and depending upon so first i'm going to be creating a spy off our breakfast waiter [Music] and then i'm going to create a second spy for the dinner waiter and to allow these annotation based spies to be created we're going to have to change our test class to use the mokito j unit runner and very simply the makito junit runner class will automatically instantiate our spies and our other annotated based instances without us having to do anything so now the question is we have our breakfast waiter we have our dinner waiter how do we enable our customer order to take in these spies without us having to do anything and that is where the inject mox annotation comes in so the inject mox annotation works in a very clever way because it will scan the customer order class and it will check for where services are instantiated so first it will look at the longest possible constructor that we have which is here and then it will realize that the breakfast weight and dinnerwaiter are two services that are dependent upon by this class and then it will pass in all of the mocks that we have within our test class which are the breakfast waiter and the dinnerwaiter and also if there is no constructor here and we had setter methods for the breakfast and the dinnerwaiter down below it would then find these two setters and automatically instantiate these two services below otherwise it will just look for the individual fields that the customer order relies upon which right at the top of the class is the breakfast waiter and the dinner waiter and then it will automatically assign these two services into the customer order so now using this exact same customer order i can now run these tests and i can expect them to pass because that breakfast waiter spy is being injected into the customer order straight away so even if i go back to the customer order and i change the way that these are being instantiated so if i bring back those two setters the inject mox annotation will realize before it sees the fields that there are two setters and it will instantly inject these two spies into the customer order and i can run the second test and expect it to pass [Music] so that's a brief summary of how the injectmox annotation works and how it's very useful in scanning our customer order class or whichever class for the services it requires either based upon the constructor second is the setters and finally just based upon the fields and then it takes in all the available spies or mocks that are available within the test class and automatically injects it into our customer order to be used so now by that logic if i take away the spy we have for the breakfast waiter the customer order will no longer inject it and therefore we can expect a null pointer exception
Info
Channel: Aneesh Mistry
Views: 7,557
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, JUnit, Java, Testing, Tutorial, Mockito, InjectMocks, Aneesh Mistry Tutorial, Aneesh Tutorial, Aneesh Mistry, Java 8, Software Tutorial, Software Engineer, Mockito Tutorial, Junit Tutorial, Backend Engineer, IntelliJ, How to test software, Test Automation, testing software, beginner software engineer, learn to code, learning to code, step by step tutorial, @InjectMocks, Annotation based mocking, Java Tutorial
Id: KuqYS9reh2I
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Mon Aug 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.