Testing RESTful Web Services -2- Mocking a JPA Repository with Mockito in JUnit 5 test

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video we created a very simple test case to test our code inside of user service implementation and we decided to begin with the very simple method that is called get user so let's open the implementation of get user which is inside of user service implementation class I'll bring that class and I will scroll down to find this method get user ok so this is the method that we are going to test and it's called method under the test we are going to test the business logic inside of this method and the business logic which is outside of this method we are not concerned in this moment and for us to be able to execute the business logic and this method we will need to have an instance of user repository object created this is because user repository object is declared outside of this method and our method depends on it so if user repository is now then we get a null pointer exception we need to make sure that user repository exists so what we are going to do we are going to use Makita framework to mock the user repository and then inject it to our user service implementation class so that our get user method can make use of it and because the user repository is not going to be a real object but simply a mock object we'll also need to fake the execution of find by email' method call and make it return the hard-coded object that we need so let me show you what I mean by mocking so we have a user repository now let me scroll up here and find this user repository so I will copy it and take it to the user service implementation test here and I'll paste it and because the business logic inside of its methods is not really important to us at this moment then method under the test is different I'm going to mock it with a special annotation that comes from Makita so I will use more annotation and how important it should come from Makita and now I'll go to my you get user method I will comment how this fell or let me actually delete it so now I'm going to assign a custom input value and a custom return value for the find method inside of user repository let me show you what I mean so I'm saying when and then use a repository find by email and string then return let's say now at this moment let's first import the things that it wants us to import like for example any string it can't find it but we can paste it here so the any string comes from orc Makita argument matching any string and let's try to import one now yeah we can import when and let me walk you through this line to explain you what it does so we are saying when a method find by email is called on user repository and this method doesn't matter what it accepts it can accept any email address it can accept any strain it doesn't matter that for us what it takes in but when this method is called it should then return a specific object and it's also up to us what we are going to return and this is called mocking we are creating a mock object here and a mock object is a kind of fake class which we can instantiate and we can fake the return results of its method by giving it the desired input and we can also give it the desired output that we wanted to return back so this output can be a Java object with correct values or it can be a Java object with incorrect values depending on what we want it to return so it doesn't really matter what the method does inside and what's its business logic exactly is all we care about is that it must return as an object that we give it so if we go to user service implementation and then look up this user get user method defined by email returns as a user entity so we'll need to create a stub or we'll need to create a dummy object for user entity and ask find my email method return as this entity object so I'm going to create a new user entity and now I'm going to hard-code the values that I want it to have let's for example set user ID and let's say it's value 1 and then let's set couple of more values for its name say Sergey let's set a public user ID and it's alpha numeric value like this and let's set a sample of encrypted password again it doesn't really matter at this moment what it has let's say this is what the password looks like ok so now again when find by email method is called on user repository taking in any string it doesn't really matter what it takes we wanted to return the user entity okay so we created a mock and we stopped it with the stub object and as you can see stab is also kind of hard-coded fake object which contains hard-coded value and guarantees us that when this method is called find by email the user repository will return us the values that we requested it to return now for us to be able to call the get user and Method on user series implementation we will need to add the user service implementation to our test class and we will do it by using user service we cannot mock user service because this is the class under test and we are testing its get user method so it has to be a real class and there are a couple of ways to create it we could hard code the instantiation of this class for example we could use a setup method and then create an instance of user service implementation by using me you but if we go inside of user service implementation and check we are Ottawa in many objects to it so we'll have to auto wire user service implementation inside of our test class but because the user service implementation takes in other objects like for example the object that we're moving here this object will need to be injected into user service implementation because here we are using dependency injection so we'll need to use a special Makita annotation that allows us to inject monks into our class under test and that annotation is called inject mocks now let's import this so now when user service implementation class is created the framework will inject into the class the mock objects that it requires so let me remove this user service now and for Makita to be able to instantiate these objects there is another line of code that we'll need to add here it's called Makita annotations and then in it mocks this like this so now we are almost done we have a user service implementation class which is when created will inject mocks and at this moment we need only one mock object to be injected there because inside of get user method if I go inside of my get user method again it has one external object that is being used and this object is injected into user service implementation using dependency injection and this is why we need to mock it and make it return the result that we want okay so let's go back to our test class we have now created needed mock objects and we can try using user service and call a get user method on it to see if it returns as expected results so let's do that in the following video
Info
Channel: Sergey Kargopolov
Views: 52,288
Rating: undefined out of 5
Keywords: Mockito, Mock, @InjectMocks, Inject, InjectMocks, JUnit5, Junit, REST, RESTful, Testing, API, Testing REST API, MockitoAnnotations, initMocks
Id: SJ2hwfdFMxY
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Tue Jul 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.