Test Driven Development (TDD) in Spring Boot | Full Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone and welcome to daily code buffer in this video we are going to learn about what is tdd that is test driven development so we are going to understand what is tdd what is the definition of tdd and what it means and we also going to understand how we can create our application using tdd okay so that's the agenda of this video so without any further Ado let's get started now first of all we need to understand what is tdd and why we need tdd so tdd means it's a test driven development which means your development is driven by your test right generally what we do is we write our application we write our apis and all the actual code and for that actual code we write our test cases okay so generally what we have seen is till now is we suppose create an API right suppose we created a controller okay for this controller we created our service layer where we will write our business logic and we have a data layer where we will write the code for our database layer or your reposit layer whatever it is right and for all this layer what we do is we write our test cases right we write test for our controller for service for data layer for everything we write our test cases and we also write our end to end test cases as well which will cover each and everything so this is how generally we do now in this scenario what can happen is the time provided or the time given to write the test cases would be very less than creating your features that can happen right it's in any organization so rather than this approach where you write test cases after your code what we can do is to improve the quality of test to improve the actual code to improve the actual quality of your code we can do tdd that means you write your test case first and then on top of that you write your actual code okay so let's see what it means what is the framework for it and how we can do it so to write the tdd first what we have to do is we have to understand four steps okay the steps are red green refactor and repeat okay you can call it as a framework you can call it as a recommendation or you can call it as a step by-step process but if you follow this you will be able to write a better TD your code would be better based on the test cases that you write and this will help you a lot to fail first because you'll be able to catch the bugs in your code before it goes to the higher environment so we will understand it so what in tdd means is you write your test cases first okay so your test cases should be return first before anything you write your test cases though it's in red let it fail we will write the test case first okay suppose I want to create an API that API is fetching me list of photos Okay so before I implement the list of photos API I'll write a test case which will test that it should bring the list of photos with all the business logic details suppose you want at least one data you want data in this format whatever it is whatever the logic you need to implement the same logic you add in your test case though it is red it's okay okay that's the first step now you write your code to make this test case as green okay your test case is written now you write your actual code which will make your test case is green that means your code will be written alongside you test your application right so your test is written now you're writing code on top of that and you're also testing your application okay you're also testing your test cases that means you are creating a test case and then you are converting into your code now at this point your only mission is to make it green okay now once that is green refactor your code to make it better to make it efficient to make it reusable whatever it is we have to do do this in refactor stage okay this stage is to make it green your business logic implementation whatever it is now make it refactor refactor your code to make make it efficient and reusable and whatever it is okay and once it is done repeat all the steps okay then you again check for all the other apis like build your other apis with that build the other service layer with that and for each and everything follow these four steps okay that's the basic agenda basic concept behind tdd if you following the steps then you will be able to write your test cases efficiently and your code efficiently with very fewer bugs okay so this is the idea behind tdd there is no framework as such this is the concept that you need to implement okay so we are going to implement one of the apis with the help of these things okay so let's go we'll create one test cases and based on the test cases we'll create the API for it okay so let's go ahead and go to our springbot project so we are going to create one springbot project here so we'll go to the browser and we'll go to start. spring.io so here we are going to use Java M project we going to use springboard 3.2 and we going to say this is tdd demo okay and I'm going to rename this package as tdd okay my packaging is jar Java version is 17 and what I'm going to add is I'm going to add spring web dependency because I just wanted to show you how the tdd will follow but ideally you can add your GPA SQL and everything and you can add the test cases for all your layers okay so let's started with simple and as a homework you can build on top of it so this this is my simple project let me just generate it and open an intelly idea so here you can see that I have opened the project that is tdd demo and now we have to create an API that will fetch me the photos Okay simple thing so for that we will write test case first and based on that we will write our code so this is my simple application here you can see that this is my main package where my actual code would be and this is my test package where my test cases would be and there's already one test case available okay so what I'll do I just remove this okay and what I'll do I'll create a test case Java class and I'll just name controller within controller package I will create photo controller test okay this is the class that I'm creating now this is what I want to write now ideally I can write at theate Spring boot test that will give all the configurations for me but I do not want everything I do not want the entire context here right I just want to test this class so we will just add the configuration accordingly so what I'll do I will just tell that I want web MVC test so I'll just annotate with this and I will autoc configure this so I'll just mention autoconfigure mock MVC okay now let's write a test case here so I'm writing test case void should find all photos okay I am mentioning that whenever I I hit an API I should get all the photos available okay this is what I want to do and for this what I'll do I'll mention this is a test case okay now to perform the API call here what I need I need mock MVC object right so I'll just add the mock MVC object private mock MVC and I am going to autowire it okay so here I can write mock MVC do perform mocked MVC request builders. get and I'm going to give my URI that is photos this is what I want to call and what I'm going to expect in return so whenever I hit this API I need 200 okay status that means my API call is successful right I can mention and expect mock MVC result matchers do status. is okay this is one thing I'm expecting here and I'm expecting one more thing that I should get at least three records okay not at least I should get three records that's what I'm expecting because possibly I am passing three records and I have three records in my database that's what I'm assuming okay so I'm adding and expect mock MVC result matches do Json paath and I'm taking the root element here and I'm telling matches has size of three okay this perform will throw the exception so let me just add it here getting some error let me just resolve it I think I gave it outside so it should be okay so this is done so simple this is my test case now if I run this you can see that it is red so first step is done that build a test case which is red it's okay so if I go here okay you can see that what it is telling that we are getting 404 here that means it is not able to find the API okay so that means we do not have any API that is/ photos so we need to build that okay so let's build that so what I want to do is I want this particular test case to be run on only one controller right so I'll mention what this test four is and based on that I'll create the controller so now you can see that with test cases I'm going to write my actual test so suppose this is my photo controller do class now this class is not available right so I need to create this class so what I'll do I'll go to my main and there I'll create the class so I'll go to new Java class I'll mention controller package within that controller package I need photo controller okay now if I come here you can see that that error has been installed here okay and now this particular class has to be rest controller okay and it should support request mapping to/ photos Okay because this is what we are building the API / photos so you can see that particular thing is been done now I need to have one API so what I'll do I'll just create public string get all photos Okay this is the method that I am preparing you can call it get photos or get all photos doesn't matter okay and this is my get mapping and I'm telling return blank as of now okay so you can see that my API is done now okay so from here I'll be able to call this so let's test this so you can see that still we getting red but let's see what happened and okay one thing is there that you can see we got status 200 that means the API which we are not able to find that is resoled now that we have another error so let's try to check that what is the error okay so no values as Json path dollar so that means we are not able to or we are not sending you can see we are not sending anything as a Json here right so what we have to do is we have to first send the Json data right what we are going to send okay that particular thing we have to build So within this photos controller what I'll do is rather than returning string because I am expecting here three records right that means I need to send a list okay so here also rather than sending a string I need to send a list right so what I can do I can send a list of what I can create a model generally we create the models right so suppose I'm creating a photo here okay so this photo is not available so I need to create it so let me just create it I'll create a new Java class and I'll say model do photo and this is what I'm going to create as a record here okay so I just created a record of photo and within this photo I am adding some of the parameters okay so you can see I added integer ID album ID title URL and thumbnail URL okay so these are the data these are the input that I'm giving so this will be the data type so if I go here to photo controller let me just import photo now because I have created a photo now okay let me just add the list as well and here I can send the list back so if I create the object of list of photo as photos equals to new error list okay I can send the photos back okay now if I run this I should still get the error because I have still not s suffice this one I suffice this one okay that my API is there which will going to send HTTP okay result but I'm not getting three records here so if I still see okay I have still the same issue okay expected a collection of size three but the collection size was zero so now you can see that that error is also gone where it was saying that it's not a particular token it's not able to particularly identify the G right so you can see that how we are solving this now let's go and add some data here so what I'm trying to do is I'm adding some data So within photos object photos. add I'm creating a new photo where I'm passing the data this data I have taken from Json placeholder so you can take any data from there from there I have taken the photos object you can create any other object as well okay so I just pass bunch of data here and accordingly this data should be populated and should return back so I was expecting three records so I have done three records here okay so once this is done if I go back and if I rerun the test case here you can see that my test case is passed right so that means whatever the test case that I had written all the business logic there accordingly my code is been working right so now what I can do is the next third step is refactor so are there any improvements that I can do are there any improvements that I can do in code yes rather than just passing the data directly I can fetch the data from my service layer and from my service layer I can get the data from database that's the Improvement I can do right so that is one thing I can do then again test it is there any again Improvement I can do again do the same thing so this is how that four step will work that is create a red test case make it green refractor it and repeat the process so this is the all idea about how you write the tdd and how improve the code quality and test quality so this was all about the tdd which I wanted to share with you it's really easy to implement you can see this is just a concept that you need to think in mind so it's not about just completely shifting to another framework it's just like shifting your mindset rather than just starting your implementation your actual code think about writing your test case first and accordingly build on top of it so it will just improve a lot better your code quality and your test quality and it will benefit you so that's it in this video If you like this video give us a thumbs up and subscribe to my channel for the upcoming videos you can also click on join button to join my channel me I will see you in the next video till then Happy coding bye-bye
Info
Channel: Daily Code Buffer
Views: 13,658
Rating: undefined out of 5
Keywords: tdd, spring boot, java, test driven development, spring framework, what is tdd, how to approach tdd, tdd in spring, tcc in spring boot, tdd vs bdd, tdd tutorial, test driven development java, junit 5, junit tutorial, test driven development in spring, test driven development in spring boot, how to write tdd, test driven development in software engineering, java (programming language)
Id: oMuaRejdSlI
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Tue Dec 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.