What is JUnit? | Why Mockito?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is David ready and in this video we're going to talk about what is J unit and why do we need mochito in fact if you have been in Java from a long time you might have heard about junit right or maybe you have worked on junit and you might have heard about uh mochito so what exactly marketer is and why do we need it so we'll start the example with junit and let's move towards why do we need Market to again in this video we are going to not not talk about how to use marketer but then why to use Marketing in the next video we're going to talk about how to use marketer so what we will do is let's get a sample project and while doing that let me just create a maven project of course now it is Maven project because I need those two dependencies which is junit and Marketo so let's go for Maven project I will say I will need a simple quick start project I don't need any web application and will give an artifact idea as a project name we'll say this is demo uh junit again with Market of course but then let's not have a big big name here and let's click on finish now once we got our project in this area you can see we have other project as well let's ignore that let's focus only on our project which is demo junit now what we will do is first like let's try to understand why do we need a testing or generate framework if you know that's great if you don't know what happens is whenever you make a project in your project there will be lots of classes right especially if you're if you're working in Java so you will be having lots of classes and in each class you'll be having some methods right now what happens is when you build an application you have to make sure that you have you you also test an application how do you test it so the one way is you can build the complete application and you can test it but you know it is not that flexible to test the entire application if something goes wrong how would you know what went wrong so what we do is we test the individual units what is unit a smallest part of your application now in terms of java classes and methods they are what you say the smallest part right so methods has a behavior right so you're testing that behavior so what we will do is we will test the classes and the methods that is called as unit using junit you can do that so you you can achieve unit testing in Java using junit again there are lots of other testing framework available but then we will be using junit here luckily if we talk about Maven project if you if you expand your memory dependency you can see we by default we have junit so we don't have to worry about that so what we will do uh now how do we actually Implement that so let's say if you have okay there's one more concept which you should know is tdd which is test driven development normally what happens is when we make a project and we test it right uh the another approach is before baking a module create a test case so it's something like you have not built a thing but then before building that stuff you have to test it okay you have to get a test case of course your test case will fail right because if you are testing something which is not there of course it will fail then you have to create a module which should work right that's then it will be passed so first your test case will fail and later it will pass but again let's not get into that way because again this video will be available if I go in that way so let's create a simple a very simple class here to test again I will not be using any complicated stuff here let's say I'm using a calculator I don't know I love this example of calculator for every example because I believe you know let's make it as simple as possible right so let's use calculator and of course when I say calculator you will be having some basic uh keywords there are some basic features right so every calculator will have a method which is addition so what we will do is we'll say public and add and we'll say this is int I comma intj and this is your add method which returns I plus J now how do you test it how do you test this feature of course you can call this right so from your main method so in your app you have your main method so what you can do is create an object of calculator and call add method your job is done right but then that is not how you actually test because that is manual testing because if you are checking by yours data is manual let's automate it because you might have thousands of classes in your application right and if you change one class it may affect other classes as well can you test all the Thousand classes manually that's not possible right even if it is possible it is not flexible so let's automate this stuff what happens is even if you change one class it should test all the classes and for that we need Automation and of course for that way we can use J unit so how do you test this calculator so what we can do is we can in your test package if you can see we have a main folder here and we have a test folder in this let me create a test case so I will say you can see that when you right click and say junit test case it is there in case it is not available so it is available let's click on this and I will name this test Case by naming convention we should have test calculator because you are testing this calculator right so it's a test calculator I will click on finish and okay so we got this test case but normally okay let's not use this extent where this there are multiple ways of doing this we can use annotation way right so let's use annotations here just to make our work simple what I will do here is I want to test the addition method right so we should get a test case I will say public void we are testing the add method so I will say test add now how do you test it uh okay so what we need is to test the calculator object first we need object of calculator that's how we test it right so we'll say calculator c equal to new calculator but normally what we do is okay let's just have this calculator there and here what we will do is we'll say okay how do you test it now to test a calculator we need to say address test because we are testing a method right so this is your first test case but as you can see we are getting some issue I guess the problem is it is with the version of genuine it so let's use a latest J unit but how do I get a latest G generators I will say I will go to Maven repository and I will search for j9 because we wanted we want latest one okay let's search for J unit and let's pick up this 4.11 and let's go back to our bomb file because this is where you will change the version right so I will just go to Power maximum file and let's use the latest one so let's remove the dependency and let's put new one okay and now once we got a new dependency you can see now we got that annotation available so in your memory dependency as well you can see we have J unit now let's say control space and let's import junit and here how do you test it now so in this we have something called as assert equals now assert equals is a static method is that assert equals okay it is giving it's not giving you the option let's say assert equals and okay I guess we have to import okay so normally what happens is this assert equals is a method of assert class so let's use a studying Port because every time you have to say assert dot assert equals that doesn't make sense right so let's use the static import now assert equals will take two parameters the first one is the okay let's say control space the first one is expected the second one is actual so I will go for actual first I will say when I call C dot add by passing two values two and three it should return five so I'm expecting five if your logic is right of course this trash will pass right now how do you test it uh okay so let's run this uh do we have to run it as normal Java application no because we're testing it right so I will right click here I will say run as you can say it's giving the option of junit test so I will say click on generate test the amazing part is when you say run you're running one test only and one test passed and you can see a green symbol there so this green line means your test passed and that's the beauty let's say if I make uh if you if I make a mistake let's say in calculator and normally this is not the case right we will not make a mistake in calculator but let's say by mistake you are saying I plus J plus 1 that's a mistake right and now if you test it now if you run this test once again you can see we got red line which means your test fails your expected value was five and because of some uh because of some what you say the coding issue or The Logical the algorithm problem you have made a mistake here and the test fails now you know okay something went wrong let's again do some modification oh you realize that we should not be having one plus one there so this is the right logic and again you will test and you can see we've got green this is junit right again this is the simplest example you can think in fact in junit as well we have uh we have before we have after now why do we need before let's say uh the object which I've created here is an open object right let's say if you want to Define if you want to create an instance inside a method normally we create that method something like setup so we say setup because this is a method where you will create all the objects something like initially this will be null and later you can say c equal to new calculator but then when to call this so you can say before the testing of the units you can say before so before is this will be called before test and you can only also have after let's say if you want to remove release some resources you can do that right this is J unit okay so this is perfect right if you even if you have thousand classes we can test all the classes using geonet okay so that makes sense but why do we so this is J unit right simple again this is not a complete season I just wanted to show you why do we need Marketo okay now why do we need a marketer here uh okay let's think about it let's say we have a calculator right and by some means I don't know why but let's say we have one more class and that class is let's say I will have a class and I will say this is calculator uh Okay calculator service or maybe calculator service here now it may happen that you you uh and this I don't want to make it class I want to make it as interface so let's say we have this interface calculator service and in this interface you actually have a method called as ADD so public and add okay which takes two parameters I will say int I comma and J now you will think okay why do we need this just think about it this is a simple application right calculator and that's why you're defining it but sometimes you might want to process this information using cloud service let's say if you have a Cloud Server and that Cloud Server says Hey programmer if you want to add two numbers you don't have to do that by yourself you can use our cloud service right so every time a user clicks on the calculate button your application will connect with the server and you will get the response the second way second base second uh scenario is let's say you are fetching this data from database maybe uh in this calculator service maybe this is not a calculator server this is something you will connect with database now what will happen is every time okay so let's let's write it here so that you will get a good idea about it so let's say if you have character service let's say this is service and of course I want an object of this so if you want to if you want to add this what I will do is I will say service dot add and I'm passing those two values so of course a user is expecting that I will add those two numbers but I'm so lazy that I'm not doing by that by myself I'm asking a cloud service to do it for me so this is an interface maybe you can imagine an API so this is an interface which is connecting with the cloud service okay now that means every time I test this calculator because this calculator service is well tested okay so whoever has built this application maybe calculator service maybe this service is provided by Google Google have done a proper testing on calculator service so of course as a programmer as a developer of this application I don't need to test calculator service right I need to test only calculator but the problem is every time you test calculator it will be using this calculator service so just imagine every time you test it let's say if you are building an application in your application you have thousands of classes if you make one change since you're using junit it will test all the classes and even if five classes interact with the cloud service even if five classes interact with database five classes interacting interacting with something else just imagine if you are if you are testing application if you're running the application it will take 10 minutes to run the test will it make sense of course not right and why do we need we need to test the service I mean the calculator service we have to test our application right so what normally we do is we create a stuff because before without this implementation we cannot make it work so sometime what happens is your application is dependent on some other application or maybe your class is dependent on some other class object so that's something other other class object may be working with database maybe it is not even built how can you test it right and that's where we have to mock those Services now when I say mock it simply means that we can create a fake service right it is not the actual service every time we test this calculator it will not connect with the server we will test this calculator with the help of fake service or a mock service again there's a big difference between mock fake and stub we'll talk about that in one of the video but then we create a mock service what it means this is not the actual service this is a duplicate copy of your service so that you don't have to worry about Capital Service you have to only test about test your calculator that's where we need walking Frameworks right so you can do the mocking with the help of mocking Frameworks and there are different marking Frameworks available we have J mock we have easy Mock and we have mochito and mockito is one of the easiest mocking service available so in the next video we're going to talk about how do you use mocking I mean how do you do Mocking in this application so yeah that's it this is the simplest application where we have talked about J unit uh how to build a genuine test case and why do we need a marketer I hope you enjoyed this video If you enjoyed it do like do click on the like button and let me know if you have any questions in the comment section and if you have some good things do do comment that as well so that's it thank you so so much for watching
Info
Channel: Telusko
Views: 363,934
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, junit, mockito, test, case, mockito example
Id: eILy4p99ac8
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Wed Aug 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.