Do you understand this code
this is crazy this is absolutely crazy what's going on guys welcome to Amigoscode in this
video I want to show you that soon you will need to write any tests oh literally that's
amazing right so you know that I'm a huge fan of testing and sometimes people are confused
in the sense that writing code to test my code like it doesn't make sense right and today I was
playing with ChatGPT and I was like can you write tests for this code and I just found out that
oh boy this is serious let me show you okie dokie so in here I'm within IntelliJ and this source
code right here is part of the brand new course that I've just released uh called Full Stack
Professional which is about 50 hours teaching you pretty much everything about Full Stat Development
and also you learn about both angular and react depending on your preference and also security
CICD and all that good stuff so here we have some code and we want to see where the ChatGPT gives
us the tests because once we have the tests then you know that we test it mainly copying pasting
and then changing few things once we have the happy path so here you can see that this customer
grow mapper we have a customer class and whatnot so what we're going to do first is let's go to
customer and I'm going to take this right here so this class I'm going to paste into ChatGPT
because I want to feed ChatGPT with as much information about my application or the classes
or the domain in order to write the correct tests so let's move on to ChatGPT so if I just paste
and then here I'm going to say do you understand this code
so it says yes it understands and now let's take the class I want to test and then see
whether it can write the test for it so here let's just take everything and then paste
and then say can you write tests for the above class using Junit5 and Mockito let's just see
whether it does the right thing so here you can see that it has CustomerRowMapperTest and
it's actually using mocking and it finished now in here let's just take this code
and what we're going to do is the ideal scenario is that we want to take the code
copy and it should just work so fingers crossed if you enjoyed this content take two seconds and
smash that like button as it helps the channel as well as the YouTube algorithm so that we can keep
on growing and providing you with awesome content like this so let's just write the test for it
and let's paste everything and obviously it doesn't right so here it's actually missing
the package that's fine and wow it looks like it did a good job right so let's now
run the test and see whether it works so let's just run the test
this is crazy this is absolutely crazy wow so it gave us the test the only thing that
it missed was the package but apart from that just have a look so it did the setup for us
it has one test because this class only needs one test
all the mocking stuff and because we gave it knowledge about our domain it knows what the
customer looks like so this is awesome so have a look so the ID is one it also passed the ID
inside of the row mapper so this is unbelievably good so this is beautiful beautiful stuff now
let's try and test something more complicated so here customer service so here we have a bunch of
classes so what we want to do is so it knows about the customer so that's fine so let's feed Customer
dto because this is part of basically field tests and I think this is pretty much it so
this is a record so CMD+C go back and then here I'm going to paste that and then say do
you understand this Java record so let's just see what it says so yes it understands the travel
record it has all the roles and whatnot very good now let's feed the class and see whether
it can write a test for us so back to the customer service class in here so customer service command
a command C and in here let's just paste and then here I'm going to say do you understand this code
so this is basically prompting and I'm just trying to make sure that it understands what I'm giving
it before I ask him the end result so he knows basically what the class is all about which is
perfect so now I'm going to say cool can you write can you write tests for this class using
all the inputs I gave you use J unit 5. and mojito
also mock all dependencies so this is important right so basically I know exactly what the test
should look like but I don't want to write the test so I'm just going to tell it what
I want the test to look like so mark all dependencies so now let's just press enter
and it's basically giving me the test class so this is beautiful so I think this we're gonna have
to say continue a couple of times because um tree basically doesn't give you the full class if it's
like too big but that's fine so let's just wait but I see that it's doing really good stuff so
there we go so this is what I was saying so continue So eventually we'll take all
of this copy and paste and see how it works right so now it's just basically continuing
and you can see that it's giving me a bunch of tests now obviously like for us we can write we
can write all of this stuff but this is much faster especially if you write for
example an IntelliJ plugin that can just give you the test class so let's just say continue
so this is the idea right so obviously you're not going to be using chat gbt like this through the
console but if there's a plug in that we could just Implement and have the tests for us and
all we have to do is just inspect and make sure that the tests are correct change few
things here and there and if we have all the edge cases covered and the code coverage looks good
then yeah so it's a win right so cool so now it finished it actually got the
last curly bracket in here which is not a big deal so let's just copy all of this
so we're going to start with the first one so copy I'm going to paste that within IntelliJ
so here let's just create the class 4 customer service
so there we go paste go back in here and take the second code snippet copy code and then
paste so we're going to continue and finally the last blob basically take this so copy and
we're going to paste cool and then the last semicolon and it's done so obviously it has
a few things I think here what it's missing is the package like before cool now let's
basically see whether if we run these tests whether they will work so let's just run
ah almost so I think here as well we have to remove gender and that's because we didn't feed
him with everything that um he needs to know about right so we forgot the record and in here run
oh my goodness everything worked apart from one test and that's basically this one here so I
think he wanted a male and we said female yes okay cool do we need to change this to female
right let's just run the failed test for a second and hooray cool so this is beautiful
stuff and to be honest so one thing that I want to do is I want to run this with coverage so
yes so here run with coverage and so we have the coverage now let's just go back into the
class which has been tested customer service and let's see which lines it did actually test
it actually missed this line in here but then we
could actually test ourselves but I think you did a pretty good job
and obviously you can see the potential of this and as you know like with Chaturbate the more you
give it the more it knows the more data we give it the more it lands the bit the better it becomes so
the future looks really really bright and yeah no need to write tests anymore that's it hooray
if you enjoyed this video smash that like button comment down below and let me know do you trust
ChatGPT to write your unit tests you can see that it can perform unit testing mocking
we didn't try integration testing but I'm pretty sure that it's capable of this is pretty Madrid
smash the like button let me know what you want to see next and I'll catch you in the next one