Writing JUnit Tests for RestController using MockMvc and Mockito | Tech Primers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] that's the bell icon on the YouTube app and never miss any updates from tech timers welcome to tech primers in this video we are going to see how we can create a unit test for others control so right now you would have seen lots of videos I would have made on creating the rest controllers but we never wrote any tests for the risk controller right in this particular video we are going to see how to create tests for the risk controller so let's quickly go and create our small basic project from the start of spring dot IO or usual website right come spring so what I'm going to do is I'm going to create a group called tests I am going to say test controller example so I will upload the project into github you can take it from this so I'm going to use the spring MVC so I'm going to use the spring MVC web so by default the test classes are generated so let's quickly generate this project the test dependencies of I default added by spring so we can reuse that dependency so we don't have to have to have specific dependencies to be mentioned in this in this particular section so we just mentioned spring MVC here the VEX project basically so the project got downloaded let me unzip it and quickly open it via intelligent [Music] and yes if you see here in the dependencies in the palm there is only one more dependency along with the spring grip which is nothing but the spring boot started test by default spring adds the spring boot start a test which internally has all the spring test dependencies so if you notice here there are lots of dependencies inside you can see the j-unit you can see the JSON path from the J way you have the market or you have the ham crust you have lots of other dependencies okay you see the spring tests from the spring creeper so all these are there by defaulting the springboard application so you don't have to worry about adding any explicit dependency so we are ready with our dependency for our project so let's go and quickly create one small hello world resource right so so that we can test that particular resource so I'm just creating a hello world resource using the rest controller so request mapping I am going to say that it going to be enter hello right and I am going to create a basic get mapping and I'm going to return some string saying hello world right so basically this is not going to return anything extraordinary so this is just going to return me a chain which is going to say hello world right so that's it so basically what we have done here is we have created a this controller but just the hello / hello and nothing and so if you hit the slash hello this particular get mapping method is going to be returned okay so let's go and quickly create the tests for this particular class right so you can do a alt enter and then it pops up to an option for us to do a create test and I am going to create a test and I'm going to use JUnit 4 so I just say je unit 4 here and I am going to run this with the spring J unit runner so there is a notation called run with from the J unit library you can have lots of runners UK you have mockito runner if you see you have more kata juta but since this is a spring project we are going to use the spring J unit for run so you just need to mention is bring J in for class candidate class in the run with annotation and now what do we need to do so we have the hello resource ID so we will just have the hello so so we need to create an instance of this so we are going to use the inject mocks so we are just going to mock this particular resource so inject marks annotation will help us in mocking this particular hello resource and if we have any other objects to be injected into the hello so that will be injected as a mock so that is why inject mocks is used and also we are not going to create the setup so I am just going to do a set up here and I will just have some method for destroyed so I will just say test hello world alright so now this particular object or created now we are going to use the mock MVC so we are going to use mock and PC to test this particular res controller so more can be see if you notice it is provided by the spring framework so with which they can test all the servlet related calls so you can have rest endpoints and then using more can be see you can call them directly you don't have to create a in-memory version of anything as such so if you are using just a client or something people used to do what they should do is they create a in-memory JC client and then they use the normal test client or the rest template and then hit that particular resting point it becomes like an integration test but this is this is like a unit err so spring has created a mock MVC with which make an easy test so I am going to show that so we need to create the instance of mock MVC so what I'm going to do is I'm going to say more can be see and then I need to create a instance of this side so there is something called mock MVC builders so using the mock MVC builders I am going to say create a standalone setup of this particular arrest endpoint so what is the resistant point which is nothing but our hello world associate hello resource basically I have just made it as hello source so we need to give this particular hello resource as a standalone setup and we need to build it so I'll just say build so this is going to create a Morecambe busy for us so the mahkum object is created so we have created the mock of the Hello Rousso as well now we need to just test it right so how do we test it so in the inside the test let's go and see how to do it so using the mock NBC object you can do a perform so perform is the only method which is there any more can be see you say perform and if you notice it is expecting a request builder so the request builder which we are going to use here is the mock request builder here so this is the request builder which we are going to use and you need to say get right so we had a get method correct so what is the resource move that yeah I forget so you can take it if you have a post you can do a post ok and the method name is hello right so / hello so that is what we need to hit so if you hit the slash hello this is going to hit T rest in point which we created there using the gate mapping okay and we need to now compare this value right so you can say and expect so expect is like going to assert your value so now I am going to use another builder so I'm going to say mock MVC builders so I'm going to use the mock MVC sorry you need to now use the matches so when you created you crater the builders so you need dowsing matches so I'm going to use the MOR can be savory MBC result mature which is going to have different methods so I'm going to check what is the status of this particular of this particular distant point whether it is going to be okay or not okay so I'm going to say whether this particular resistant point returned okay if there was a question point it is going to return okay which is nothing but the status 200 so for that particular HTTP result so I'm going to use another matcher and I'm going to say what is the content so I'm going to say the content should be a string so in my case it is change right so I'm going to compare it with let's say hello world right so I'm making sure this desk is going to fail because I have given HS caps so let's run this particular test case [Music] if you notice here the test cases failed and if you see the assertion says the response content is hello world but actually it came as lowercase hello so what we are going to do is we are just going to change this and you can remove all the so you can remove all the necessary one so if you want to make the test case more concise and clear you can even do a on-demand static so here also I can just remove these I can just make it like this so that it is more clear for us to see the test so I can move this inside here so this way you can have the test very neat so if you read this particular test case I can understand that I am doing a gate of hello the status should be okay and the content should be hello world and that is what I expect so I have just so I run the test case again so it should now return me the response successfully so you notice here the test has passed now so this is how you can write a test for a controller so in this case it was a simple string rate so what if it there was a JSON message right I want to compare some JSON so I am going to create one more resistant point which I am going to call it as Jason okay and I'm going to return some JSON of type hello let's say right right and I'm going to create this as a in that class I will just create a some basic objects right so I am going to say hello and you is gonna say title and I will create one more value okay I'll just create some constructors and getters and setters okay so we've created a constructor on the getters and setters that's it so here what I'm going to do from the J's and I'm just going to return some new juo object right so new hello objects oh hello object is going to be this it's going to have the title and the value so I am going to say title should be hello world or title should be [Music] greeting okay and I'm gonna say value as hello Would right and this is going to return now this is going to be a JSON message so I'm going to say produces here media type as JSON okay so this is going to get a giver JSON response which is going to have title and value and we are going to write a test case for this particular method so I'm going to quickly write that same way so I am going to say public word test hello world Jason right now I'm going to use the same Mach MVC I'm going to say perform as the same way it's going to be a gate so I'm going to take it and in this particular case it's going to be JSON so that is the Patrick and also I am going to say accepting content which should be only of the type Jason so I'm going to say media type job Jason Jack sub only this particular type content so let me go through exceptions later okay so now we accepted responses only of the digestion now we need to occur them back so I am going to say an expect status should be again is okay I need the status to be fine it should return the message and now in the content I need to compare the Jason right so previously we used the content instead now we need to use the JSON part so if you notice here by default IntelliJ is suggesting me that the JSON path is coming from the mock MVC result matches similar to how we got a Content initiator so I am going to use the same thing and here I need to represent the JSON message as a dollar so if I say dollar I can access the JSON method so in our case adjacent message was having the title and value right so we can access directly like this so we can access something like dollar or title and I am going to say I'm going to say title should be equal to greetings that's what I eat it right yeah greetings so I'm going to say this title should be now matching right and I'm going to also compare the value so I'm going to say dollar dot value and I can just say matches dot is so again hello world right I'm going to just end this and then let's run this particular test case so this should again return a successful response because I've just accepted the title and the hello 1 so this is exactly the JSON message which we're easy so you are directly comparing the JSON message with T response which we got so so yeah if you notice here this is successful we have received the successful message from the rest endpoint here which will turn the hello so what if you want to check the size right so what if you did not know the JSON length so in that case you can do if you had a list you don't have to mention anything you just say this and then you just say hash size so you can just check the size so you can just say this however in our case it is not a list right we are not returning a list of less Jason so I am just going to say dot star so if you do this is going to return me so let's say for simplicity I'm just going to make it 1 and make this particular test case fail so that we know it is correctly and setting so right now we have the size as to because title and value is there so the size should be two this should fail now yeah if you notice here the test case is paid if you see the collection is saying the expected collection size is one but it is 2 so as we expected it is to only what we made it to fail just to see how we can compare the size of DJ's and message okay so this is how you can test and write the test cases for the you test controller so hope you found it interesting and I don't know how many of you were similar with this more can be see methodology there you can use more can be to write the test for the controller so I'll just summarize what we did so the test case with this test case the started off with the run with annotation pkv spring j8 for class grammar so you can use a market or runners button in our case we are using spring so we are using a spring you in foreigner and the next step is to create the mock MVC object so as a part of the setup we are creating a builder for the mock MVC using the hello resource which we injected as a mock so using this particular mock it will create a mock NBC builder so it is like creating an in-memory spring MVC builder so it automatically creates a server kind of setup and using the mock NBC we are doing a perform using the rest endpoint you are using error standpoint using get so we are using get if you are using post you can use force or anything else here and then once it is done we expected a status of it to be okay if you if you expect it to be failing so you can do that as well if you if you want to write a sad pass these are all happy personal use which I have written two cases for you can write a sad path and then you can make it fail and see whether it is working on okay so we are taking the status here and the next one would be the content you can directly do a content again so there are matches which we get which we can use so as you see here these are more chemically result matches I which I am using using that we can directly assert the values using functional step or the Builder pattern so next if you want to check the JSON message you can use JSON path and an accuracy Jason value like this here so if you have a list you can you can use directly dollar dot that whatever title if title has a list of you can say title of zero title of one and stuff like that so this is how you can write test for a risk controller this is like a proper test for a risk controller which I had been writing it for quite a while now so hope you found this interesting and you try it out edge above plays or maybe at your home okay meet again in the next video thank you very much [Music]
Info
Channel: Tech Primers
Views: 185,618
Rating: undefined out of 5
Keywords: techprimers, tech primers, junit, junit example, rest controller tests, unit test for restcontroller, mockmvc example, mockmvc tutorial, testing restcontroller, test rest controller, test controller using mock mvc, test controller using mockito, test spring controller using mock mvc, test spring controller, testing controller, how to test controller using junit, junit for controller, testing spring boot app, test controller in spring boot, spring boot test using junit, mockito
Id: 8S8o46avgAw
Channel Id: undefined
Length: 17min 57sec (1077 seconds)
Published: Mon Aug 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.