Test your APIs in Android using MockWebServer!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Sal M friends welcome back to the channel always you UN shaeri in this video are going to learn how you can use mock web server in order to mock responses from the internet when using retrofit let's get started so the whole point of this video is to teach you how you can use a utility class that is called Mo web server developed by the OK HTTP team at Square I think yeah and let's pretend you do have some use cases or some view models or some repositories that uses internally a web service for example that uses retrofit in order to accomplish certain stuff in order to test that repository we can maybe mock or create a fake of that interface of the retrofit and that would work perfectly fine but we want to create a realistic mock that will mock the responses the actual HTTP responses when using retrofit so let's get started so the actual mock we server is found here like this is really cool as they said in the motivation this Library makes it easy to test that your app is doing the right thing when making HTTP and HTTP calls like this is the new thing because previously when you fake something you can for example experiment the cases when there is an error for example 400 4004 for example 203 Response Code for example you can do that you can basically return responses or throw exceptions but here you can kind of simulate the actual Network stuff and Beauty we can write this as unit test we don't have to rely on instrumented test so here is how it works mainly we are going to create Mo web server right and then we are going to tell retrofit when create think our interface we are going to tell it to use this thing and this will be running and then we can set up or inq responses for example are going to set the body are going to set the response are going to set the code the actual code Response Code and similar stuff and then you simply do your thing and then you can assert stuff like we are going to see that in a minute as I said you can specify headers this is not feasible with a normal fake or normal moito or M for cin right you can stimulate slow Network this is also pretty fine like the interface like I suggest checking this web page interface and the API is really cool so you can check it also you can specify cases or conditional like inq multiple responses that would work perfectly fine we're going to see examples right now so here first you'll start by going to the build file and here we need to add dependencies of course make sure you have the dependencies for retrofit okay HTTP gson or whatever right and we need that dependency which is mock web server and here's the thing I do have here A View model that use internally a post service this post service is nothing but retrofit interface right and it has something called Fetch post and in this post it's going to return us this post are making sure that in the gson we will have this things here variable names here right this is the post you are going to return it and in the view model we are simply delegating there is nothing special about this code but the thing here we want to test this post view model we can test this post view model model while faking this post service but we are going to use an actual and more realistic fake so let's start by creating the test you can do that and create the test for the service can use junit 5 problem and let's go here it will be as part of the unit test and we will have this thing well I'm going to use jit for no problem all right so let's first start by creating a test are going to test the normal case like returning some actual posts okay testing normal this is normal test but here we start to Define some variables first we need to Define our mock server right so it will be while mock web server like that it would be simple mock web server like following and then we need to create our post service this one the actual one and we need to create the view model we need to pass one to one right so here is how you can do it you can well I'm going to use late in it VAR for the view model because I'm going to create it late and also the post service will be also Laten it and then I'm going to use setup function in order to set everything now for the Post Service its creation is a little bit different because we are relying on retrofit to do that okay so here in retrofit there is a builder and the normal way to create retrofit instances right so at the end we have to do the build and you have to do the create and then we can pass our post service class like that in order create but here we are going to tell here at the configuration from for the Post Service to request when requesting the server to request the mock web server and the mock web server will give it the response back so here we do the following we do something called base URL as normal here we can pass base URL we are going to use the mock web server and there is something called URL and here we can pass do slash like that awesome but here this need to be done before the B sorry after buil like that and then we can assign also a client and here we need to create client which is ok HTP client private Val which is client let's call it client and it will be okay HTP client. builder. normal build my than special okay and then we can use our client and then since you are receiving responses in gson we need to convert the responses from gson to the actual class which is or in our case which is the normal post right for that we need also to set up the converter so we need add converter Factory this one and here I'm going to use gson converter factory. create that's pretty much for the creation of the rofit this is the normal way on how to create RIT instances in our production code it's not something big and then when creating the view model we can create our post view model and then here we can pass the dependency which is post surface and here simply we want to do what we test the view model where is the view model exactly you want for example to get all the post this is just used for the sake of experimenting usually we do have an NVM aspect in which trigger things and then we receive it as UI state but this is merly for explanation purposes let's pretend that this view model will get us some result here since you are using ctin to do that I'm going to use run blocking like that and then I'm are going to do some setup and some assert so how we can do this setup first we need to create something called the mock response Okay so there is something called mock response and here you simply create it and then you attach things to it you can set the headers you can set the body you can set the response code right for Response Code it will be 200 this is normal thing and then I can set the body right and here for the body I'm going like the beauty you can do the following you can create file of gson and put them in the assets in the directory of the test and then read the files here here I'm going to use my normal string templates and I'm going to pass a whole big uh gon here is the gson I'm having right so it is user one it's a user ID title like this is something generated from chat GPT or something so this isn't something big can do it like that and maybe we can little bit something like that awesome so here is how we can set the body now what we want to do after creating that mock like this is just creation we need to record it somewhere right this is the response and then we need to tell the mock web server right in order to include that response so how we can include the response simply pass the response we have created now whenever we request something from the web server right it will get us this response back so I can assert the following I can assert that equals for example here I'm passing I think passing four posts so I can insert that they are four and then I can do the following I can check the result the result size and this should work pretty fine we can run it to see if this working now there is other things that you can specify I'm going to show you in a minute let's test pass awesome normal test is passing usually you can do three here to check if you are testing the right thing and then but I actually are testing the right thing now there is things here you can start by doing something like that and there is a method called start okay here there is this method will help you start on specific Port right usually it start by default when creating it so this is an extra method if you you want to specify specific Port right this is the first thing and there is a method called shutdown also so usually we use this methods both at the start and at the end on on the tear down okay and now we can specify another response let's pretend we do have a problem in our HTTP response okay let's pretend it is for example an empty response or let's say exactly we can do something like that which is bad request like that and you can specify for example 4and it okay and test for example failure when post fetching okay that's pretty much it and here since our get all post it won't work it will throw an exception we need to specify that you are waiting for an exception usually we do that here and the exception expected exception we are waiting for it is the following we are expecting HTTP if I remember well HTTP exception right I think we can specify HTP exception but I'm going to see if I get the exception or not okay so I can run this and see if I'm getting an exception and using this kind of mock web server we can do something great we can do test driven development in a very good way we can drive the development of that view model in a good way since we can simulate a lot of things related to http Stu here you can see the error HTTP 400 Cent air okay so we need to see if you are getting that air either we have to approaches here since I'm not going to modify view model itself usually here we do something like following we will do some try and catch and see if we have problem right let's catch HTTP exception not H exception HTTP exception like that and here you can see if it is I'm going to return empty list like that while I can't return anything and now what I can do I can't do the following I can assert that we have an empty list assert true that the result is empty okay now I can run all my test and see awesome all my test are pass you can do it that way or you can specify here in the exception but usually in case development you won't have that error you need to specify how you can solve this issue but you can build this actual code here and the algorithm based on the different tests you can exercise when doing test G development of course okay that's pretty much you can see a lot of things going here like there's a lot of things related to mock web server right as I said for for example you can do throttle body this will simulate slow Network this good thing for testing timeout and you can use of course this thing into instrumented test right so if you want to have some end to end test not end to end test usual end to end test you would use the exact implementation and the exact production server but at least in integration testing and big kind big chunk of testing of UI testing you can use this thing in order to test variation and the happy path B path of your UI okay and uh as I said there this dispatcher this is good thing to serve and to use many responses from the request this is good thing and also I think yeah definitely you can check it this would be really good thing but here for example if you can inq many things for example this one this one this one so the first one you are going to get this thing the second you are going to get this thing and third time you are going to get this y do okay so this is pretty much it for this mock web server I hope you understand it if you have any question please let me know in the comment below thank you very much for watching this video to the end don't forget to subscribe to channel have a great day and see you in the next video Salam
Info
Channel: Charfaoui Younes
Views: 1,265
Rating: undefined out of 5
Keywords: mobile development for beginners, mobile development roadmap, mobile development vs web development, Jetpack Compose, Android development, Permissions, App development, Tutorial., UI design, Android Studio, Kotlin, Mobile development, Best practices, testing, UI testing, unit testing, app quality assurance, Android app development, productivity tips, software engineering, best practices., test, private, mutation
Id: c9leA895cNc
Channel Id: undefined
Length: 12min 3sec (723 seconds)
Published: Tue Nov 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.