Test Driven Development (TDD) | Crash Course | 2020

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome to my channel my name is Nelson and in this crash course I'm going to teach you what you need to know about test-driven development if you're new to my channel go ahead and subscribe and also give me a thumbs up so I can keep on providing courses like this so also if you if you're not part of the amigos code group go ahead and join because we are now over 4000 people learning how to code together and the community is just growing every single day so in this crash course I'm going to take you through the journey of learning test-driven development testable development is something that you as a software engineer shouldn't know how it works because make you a better developer so there are many benefits just by using test-driven development so for example you spend less time debugging your code because your code will be actually solid right so implementing two interfaces as we all know interfaces are always good in terms of mocking the services and external services and making sure that your tests are fast right so that's what we need and there are many benefits you know reducing costs and all all the good stuff that test-driven development office so in this crash course what I'm going to do is actually teach you how to test this little part right here where this is actually part of the entire full pores right but in this crash course I want to teach you how we are going to use TDD to test a feature inside of this payment unit right so how does development will drive your actually how this test drives your development so that's that's what TDD is right and you'll see you know it's it's not difficult I'm gonna break it down super simple for you so that you come out of this crash course with some knowledge so without further ado let's get started all right so to follow along with this course and make sure that you actually practice as well what I wanted to do is to navigate into this github repository where I'm going to leave the link in the description of this video so go ahead and simply clone this repository so this contains the entire code for the testing course so right here so this is the actual diagram so basically in this crash course I'm gonna cheat I'm gonna show you a subset of this payment you need and how to perform test-driven development so let me go back so I'm gonna do is clone this somewhere press command-c and then you can either use IntelliJ or your command line so make sure that you have git installed so right here I'm going to simply say get and then clone and then paste the actual URL and and actually before I do that let me simply CD into desktop and then run git clone and then that there we go so now I do have so in my desktop if I do an LS you can see that we have software testing now inside of software testing go ahead and CD right so change directories software testing and you can see that I'm inside of master so this is the actual master branch so what I want to show you is so right here I've got a couple of branches of all eight branches so if I click on branches there we go and if I few more active branches you can see that I do have the starting point payment stripe so on and so forth so this is what we're going to complete by the end of this crash course so the branch 7 so 7 - TDD so what I'm going to do is to switch to this branch right here right so 6 and an integration testing so what I'm going to do is actually open the project with IntelliJ so IntelliJ and then open and inside of my desktop I'm gonna open the software testing folder and then open the palm to XML open and then as project and there we go so if you don't learn more about intelligent go ahead and check my website or my YouTube I've got a full course just on IntelliJ and it's pretty much one of the most popular courses out there for learning this awesome IDE so right now you can see that if you check my cursor probably so right here I can click on this button and this will bring up all the remote branches including local branches but the branch that I want to check out is this one or region 4/6 integration testing so check out and there we go now to make sure that things are working correctly for you go ahead and open up the software testing folder and then source and then test and right here you should see that we have calm the amigos called testing and right here this is basically a bunch of tests that we wrote for so this entire thing right so this course is packed with awesome content unit testing integration testing mocking external services testing the database and a bunch of other cool stuff so this is everything that you see here so go ahead and make sure that you can run every single test so right click on the package so calm amiko scored testing and then run tests so this should work flawless for you so just give it a second and there we go so if you see that we've Hall couple in the testing and some integration testing going on and everything works so if you can get everything up it means that everything has worked for you and he have successfully checked out the code correctly so I'm gonna collapse that and now inside of the main package rice of main and in Java and then the actual package so this is where we're going to add a new package and then learn about test-driven development this is all for now catch me in the next one all right so let's go ahead and switch a little bit to learn how test-driven development does work so as I said we haven't been doing test-driven development because I think is best if you see how testing works instead of switching to instead of learning actually TDD because I feel like TDD is a technique that you should master once you know how testing works right so let's go ahead and create a new package right so what I'm gonna do is actually inside of the test folder right so remember with TDD we don't write the actual class right so we start with the actual test so package and then let's call this you Till's and then inside of utils go ahead and create a new class and simply say phone and then number valley data and then test and then press Enter remove the public key word which is not needed and there we go what we're gonna do is actually um create a test and then create a class and basically we want to go through through a phase where we first create the test and then we create the actual class we run the test it should fail and then it goes green and then we actually refactor right so I'm actually give let me show you what I mean so right here in this picture this is the actual test-driven development cycle so we kind of star we'd a failing test we make it past refactor and then the test my fail we make it pass refactor so on and so forth right so this is how test-driven development works and I want to show them and I want to show you exactly how this works so let's go back to IntelliJ and right now so this is the actual building blocks right so we have a testing class we have no code whatsoever right no code whatsoever and and now we can implement the actual full cycle right so what I want to show you actually is so this phone number validator class that we're going to create a second this will be used inside of the customer registration service so right here so you see where we have the phone number right so we're not doing any validation right here so let me add it to do here right so we're not doing a validation so to do and then validate that phone number is valid right and this is why we are creating this phone number validator test and also the actual class which we will create in a second so this off for this video if you have any questions go ahead and drop my message otherwise let's move on so with our test what we wanna do is create a test that says it should and then Valley date a valid actually let's simply keep it simple it should validate phone remember right so there we go and right here remember what we need to do is actually do the following so inside of the setup method let's go ahead and create a private and then phone number validator right there we go and actually sorry let's call this under test as we've been doing and now we're going to say and the test equals to new and then let's grab this and there we go right so you can see that we don't even have this class right so if I try and run this so let's just try and run this test you can see that you know it hasn't compiled right so we get a compilation error and this is because the phone number validated class does not exist and this is part of test-driven development right so now we need to go ahead and actually create the class so let's go ahead and create a package package and then utils and inside of utils we need a class called so class and then paste that in and there you go so now if I go back to my test you can see that if I now try and run this test this should work there we go so you see that it did work so now that we have the first step out of the way next let's go ahead and see how we're going to carry on with the building blocks of TDD all right so now that we have things compiling and you see that the test did pass a while ago right so this test right here so if I run it again you see that it's working but it's not doing anything so what I want to do is let's say given so let's say given a phone number right so string and then phone number equals and then let's simply say that this will be a UK phone number so four four seven and let's simply say one two three four five six seven so this is the actual length of a UK phone number right so we're gonna keep things very strict just to UK phone numbers but you can change this to support many different phone numbers right and obviously you should be using a library to validate phone numbers but I want to keep things really simple so you see how it works so let me go ahead and simply and this with semicolon and now right here I'm going to say under test dot and then validate right so you see that let me pass a phone number so if I try and run this again there we go so you see that we don't have a validate phone number right and also this should return a boolean so they seem to say boolean and then is valid equals two and in that and what we need to do is actually create this class so all of this is part of test-driven development so the actual testing is driving the actual development right so let me actually create the method and what I'm gonna do here is something a bit different right so right here I'm going to say this implements a predicate right and this will take a string there you go and let me simply implement methods test so this will have a test method and not validate so now let me go back and test right so this would be test so now you see that currently we are returning false by default right so what we want is to say assert that and then is valid and let's import a surfer J their dot is and then true right so let's run our test and you see that the test run but it did fail right so expect it true the actual false and this is big calls right so this is because of this oops so this line right here right so this is when now we actually start implementing the logic for our phone number validator so I just wanted to see exactly how things are structured I'm going to take baby steps in each video so you understand exactly how test-driven development works so now that we have things compiling and the actual method that we want which is which is the test next let's go ahead and implement the actual logic to validate any given phone number poor the logic of our phone number validator as I said this is only for UK phone numbers and if you want to test for you know why the numbers go ahead and use a phone number validator library I think there's one from Google which is quite good to be honest so I wouldn't recommend you to implement your own phone number of validator but as I said I wanna show you exactly how test-driven development works so right here let's go ahead and simply say that so right here we want to make sure that the phone number so instead of s this will be phone number right so I want to make sure the phone number so phone number dot and then starts with right so starts with plus and then four four and I think I'm just gonna leave it there right so I think this will be a good building block right so I'm gonna say if so if and then phone number starts with that right so I'm going to return and then true otherwise false right I could actually inline this or simply say return and then remove that and basically this is the exact same thing right so now let's go back to the actual test right so let's go back to the test and if I pretty much just run this you see this works right so now what I want to make sure also is so let's say that this is like plus four - right so if I run this you see that the phone actually the the test fails right so it is false so let me go right and I also want to make sure that so right here and right and phone number dot and then length equals to thirteen right and that would say my column and let me put this on a new line so you see exactly everything right so now if I run this you can see that it still pauses so now let me go ahead and pretty much just add another counter there and then there we go you can see that it fails right because the phone number right here so the phone number has 14 characters so there we go so you see that this was a very trivial but obviously this logic right here is lacking lots of things right so we want to make sure for example there is only digits right so we're not checking for that but I want to keep it super simple super simple so you see the entire process right so now I could go in and pretty much just grab all of this right and then right here should validate phone number when incorrect and here that simply you know and a random number there and basically so this now is false right so we testing here actually isn't correct and has length bigger than oops than 13 right so we could actually use this annotation so at and then display oops contract today so a display name and basically now you could have bigger than 13 right should fail when length is even bigger than 13 and there you go so now we could take this and then right here so let's move that and this will be so basically it doesn't have the four actually plus sign when does not start with plus when does not start with plus and then sign right so now we can go ahead and run every single test in this test class so run just give you a second and you see that we have three tests dot did pause so there we go so this is a taste of test-driven development right so obviously if you were to let's say now you want to say that you know there is an extra thing here or let's simply say that for example you changed your mind and now it should start with for example this right so zero plus four four and then forty five right so now if I run the test this should fail you see that we have a two that actually did work but there is one which fails so this is the actual happy path right because the starts with is different so this is actually the actual full cycle right so you change we you refactor you make it pass so on and so forth so this is actually test-driven development now let me go back and I think the logic is is not what I want I'm just gonna leave like that and this is all for this video so next I want to show you exactly how you are going to be able to condense all of this into one single test and have parameters into your test which i think is much neater all right so let's improve this class right here and pretty much just have one single method that we can pass multiple parameters so this is what we call as parameterised test right so right here what I want to do is the following so here I'm gonna say at and then parameterised test and it's not going to be a test anymore and then right here at C s actually see s oops see as V source just like that and right here so command P you see that this takes a value so the value will be it's like an object right so this will be an object where we can pass the actual values right so actually we we can pass we can pass the actual input and the expected value so right here so let's form from this test so I'm gonna grab this put it there and I know that for sure this will be true right so true and let me put this in quotes just like that and let's begin with this only test right and then we can change the rest so now right here this will take a string and this will be input and then this will be a boolean and then this actually I think people have to be strings I'm not quite sure but we can test so string and then expect it like that and this actually has to be within the exact same string right because we can have multiple ones so for this phone number we expect the valley to be true right so now right here I'm going to take the input right so input and in fact let's change it to phone number so number just like that and now I can remove this right so when we test the phone number which is this one right here we expect that right so I expect that is valid and then is equal to and then boolean don't value off and guess what I expected right so now let me go ahead and try and run this test to see if we haven't messed up anything so there we go you see that it works and right here I hope you can see I've just zoomed in so you see that the first parameter is the valid phone number and the expected value is true right so the test is working so if I also say false here so false run it they should fail there we go it fails right because the phone number is valid right so let me actually put that to true and let me just I'm just wondering whether I can pass a boolean here right so true let's see if this does the casting for us so true and then I don't need this so I hope this works so let me run it and oh wow it works so even better so now what we can do is simply take this phone number right so let's take this whole number and then right here so we can let me put this on a new line just like this and then comma and then put the phone number there this will be false comma and then let's grab this phone number and there we go this will be also false and basically you can improve on the and have you know the actual error message why it's not valid but I just want to keep it simple for now and also I think I forgot to show you so when we run the test right so let's run this test right here before I remove vit right so you see that the display name so this display name is what shows in here right so right here should fail with plus right so if I remove that it simply shows this as the actual display name so now let me go ahead and remove all of this all of these two tests and check it out so isn't this neat have a look beautiful now I can run one test and pass a bunch of arguments and I can have my parameterised tests working beautifully so if I now run it and see that it will work and you can even investigate you know the actual inputs right one two three and you see the numbers and the tests are passing so this is the beauty of parameterised tests if you have any questions go ahead and drop me a message next let's go ahead and actually add this phone number validator class inside of the customer registration service right inside of this class so what we're gonna do in this class right to make sure that we actually add the phone number validator is the following so in here let's go ahead and simply say private and then final and then phone number validator right so let me simply add the constructor to parameter and I'm going to refactor and this is because we have to fix the test in a second so there we go let me put this full screen now right here what I'm gonna do is the following so if and then phone number validator dot test pass the phone number right so if it's not valid I'm going to say throw new illegal state exception phone number and then plus phone number plus I could use format but whatever is not valid right you could improve on the error messaging here but I'm gonna keep it simple now I can remove this and now let me go to the actual test right so command shift T and now guess what we need to pass a mock right so we're going to mock this because we already tested that the phone number validator is doing what we want so add an mark private phone number validator and there we go now let's just for the sake of it try and run every single test in this class and IMAX and I'm expecting things to fail and there we go right so phone number is not valid for this one this one this one and basically in C right it's failing all over the tests so let's go ahead and fix one by one so starting we'd it should save new customer so right here so what I'm gonna say is valid phone number and then given and then phone number of validator dot and then test and then phone number and at this point we don't really have to make this a valid phone number because we are mocking it right so dot will return and then true right so we want to make sure that we have valid phone numbers all around so right here and right here and also right here right so now if I run all the tests so let's run all the tests you can see that they now work but what we need to do is add one extra test because if I run this in what is it with coverage there we go if I collapse this in this go back to the class and the test you see that we have this red line which means that this isn't tested so let's go ahead and add a test that covers this if statement when it falls and this line right here so let's go back and if I scroll down so what I'm gonna do actually is I'm gonna copy this entire test so it should save new customer and I've been saying we tests you will copy and paste tests around and then change them slightly so it should and then not save your customer when phone number is invalid there we go so now what I need to do is you see right here so given phone number validated test phone number will return false right and right here I know that so right here so I'm going to delete that and then should have no interactions just like that oops so basically so if I go back right so here so if the phone number is not valid we will throw an exception therefore there is no interaction with the customer repository so this is what I'm saying here right and now let's go ahead and run this test oops so phone number is not valid and why is that I'm actually yeah sorry so I'm actually no asserting what I want to assert right so basically I want to assert the actual exception so there we go so we want to certain that the exception is thrown dot has actually start with instance of illegal sale exceptional class dot has message containing and right here you see that this is a message phone number and in that so let me paste that in whoops just like that and right here let me simply replace this with two pluses and then pass the actual phone number there we go now if I run the test that was funny there we go so it passes and also we don't need and this given right here so actually let me go back so you can see so this one here right so this one so given customer repository because we are saying that there should be zero interactions with it so let's make our test a bit faster and there we go so now let me go and run this with coverage there we go now if I collapse this and this and if I go back to the actual service and now you can see that this line right here so line 27 has been tested so we have a good test coverage so the last thing that we're going to do is actually so we kind of made some changes right to the way our application works so let's actually run now every single test including our integration test and see whether we have broken actually and see whether we've broken something already to fix some tests right so let me open the project tab and right here so I'm gonna run every single test so under this folder right here or think we can run why she doesn't recognize so here so what you see Java so this green folder or you can run the actual main package right so it doesn't matter so we want to run every single test in here so let's do that so run all tests now we have a bunch of unit testing and integration tests so let's see whether we broke anything and there we go so we've broken the actual integration test so it should create payments successfully so this is actually broken so let's investigate why and [Music] and it seems like we have an error saying no qualifying beam of type and then phone number validator so let's actually I think we're missing the actual annotation so this is the benefit of having tests all around right so inside of is it where is it so yeah so customer registration service right right here so we are using this phone validator so let's go and open it and we are missing of course the aunt and then service annotation let's now run the tests again so you can see why having tests is so important guys just give a second and it's still failing so let's see why this time so now there is an exception and there we go so phone number zero zero zero is not valid which means that so let's open up the integration test quickly so under payment payment integration test so right here so this is where we're actually testing the entire application right so making sure that everything can integrate correctly so here obviously we have to now add a valid phone number so plus four four seven one two three four five six seven eight nine and I think this is it so let me actually now run all the tests again one last time and hopefully this should work so right click and then run all tests and just give a second hopefully everything works now and there we go so you see that every single test passed which is amazing right so you can see that we have a bunch of tests that we've written throughout this course so now hopefully you can start to see the benefit of having unit testing and integration testing and basically testing your application to make sure that it works right to make sure that it works so if you have any questions on what we've done in this section go ahead and drop me a message otherwise catch me on the next one okie-dokie I hope that you had fun learning test-driven development and hopefully this was just a small taste but I've simplified a lot of things and you can see that those are the starting points right so if your service class becomes bigger and more complex you you try and you know write tests and then the tests will drive the actual development right so go ahead and actually you know experiment with our examples and what I recommend for you next is to check out my full course on software testing so which software testing I will teach you everything that you need to know about testing so we're going to learn about unit testing integration testing how mocking works using assertion libraries testing your database way so the queries that you have testing external services such as stripe and twiddle and a bunch more so in this course right here so software testing it will make you a complete test that right when it comes to and you know making sure that whatever you write to production is tested properly right and also I'm going to show you how you know coverage or testing coverage works with IntelliJ to make sure that you actually are covering as much as possible so if you're a to my channel go ahead and subscribe and also give me a thumbs up so I can keep on providing content like this this off for now I'll catch you on the next one so now Molly
Info
Channel: Amigoscode
Views: 49,037
Rating: 4.9089971 out of 5
Keywords: test driven development java, tdd, tdd vs bdd, behaviour driven development tutorial, behaviour driven development c#, software testing course, software testing life cycle, java tutorial, test driven development javascript, junit5, mockito tutorial, mockito spring tutorial, mockito spring boot rest controller, spring boot microservices, amigoscode, spring boot integration test, spring boot integration testing tutorial, spring boot integration with mysql using spring jpa, coding
Id: z6gOPonp2t0
Channel Id: undefined
Length: 43min 53sec (2633 seconds)
Published: Mon Jun 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.