 Hello everyone. So in this video we will be talking about using JUnit testing for Java applications. Specifically in this video we will be using Eclipse IDE in order to run JUnit test cases. So let us see what is JUnit and what is unit testing and then with the help of Eclipse IDE and with the help of live program we will see how we can create a test case for our chosen function. At the end of this session we will also talk about how you can write complex test cases. At the same time we will also see about some of the fundamental features which Eclipse provides us in order to effectively implement JUnit test cases. So let us move ahead. The objective of this video will be like that say at the end of the session student will be able to use JUnit testing framework in Eclipse IDE for unit testing of Java applications. When we talk about Java applications the Java application can be a simple console Java application or it can be an enterprise level web application which might involve satellites and GSP as well. So let us move ahead. Let us see what exactly a JUnit testing framework means. A JUnit testing framework is fundamentally an API, Java based API JAR library which is given to a developer so that a developer can write a unit test cases. So as per the standard software engineering definitions we all know that generally when we write a code, a module level testing is done under unit testing framework. A module of a source code can be like say it might be a smallest part a testable part of code. So this modular approach is very heavily used in modular programming approach wherein a major task is split across smaller sub tasks and instead of testing an entire large module we write a small unit test cases which test the sub modules. So JUnit testing framework is one such test engine specifically tailored for Java based source code modules. So let us see how we can write our own JUnit test cases in Eclipse IDE and before you move ahead in this with the following video make sure you have the following prerequisites installed on your machine. So what we expect is we expect a small fundamentals of Java language. At the same time this video is using Java 8 as its runtime so make sure you have a Java 8 installed on your machine and also make sure you have a working Eclipse IDE. So right now I'm using the latest available Eclipse IDE for this video. So let us see the live example of this. So I'm gonna use my Eclipse, I'm gonna use my desktop. I'll be switching to Eclipse IDE then I'll create a project and we'll see an entire process. So I'll be typing a code and then I'll be explaining whatever I have typed on my code here. So let us move to Eclipse IDE. So this is a general Eclipse IDE. So I don't have anything on this Eclipse framework right now. So what I'll do is in order to test the Java code I need to first create a Java project. So I'm creating a Java project and this Java project I'm creating it so as to write a small Java module which will let you know whether the given number is even or not. So once I write a method then I'll be writing a unit test case which will test the correct working of that method. So let us talk let us name our program as a demo and let me move to our next. So make sure you follow the steps as it is what where I'm doing in a window. As far as if you are completely know what you're doing, you're free to do your own way of creating project but if you're a complete absolute beginner, make sure you follow the what are the path flow which I'm following in my video. So let us click on finish. So this creates a project and under source folder, let us add a main class under which we'll write our method which will check whether the given number is even or odd. So I also want a main method within this main program. So I have checked the main method. So this is my main program. So I'll keep now here under this main method, I'm gonna define a method which will check given an integer number as input to this method. Is it an event or a odd? Let's see how I write that method. So we are inside a class. I'll write a public method here public. So I'll call method name is ease event and it would accept one integer I as a variable. Now since this method, the functionality of this method is to check whether the number is even or odd, the return type will make it as Boolean. So if the given number, for example, here if the i is even, I'll return true. If i is for if i is not number, I'll return false here. So we'll need a condition statement. We'll write a I'll open if block and here as you all know that if I mod two, we always check the mod if mod equals zero, it's an even number. Since the function is taking for ease event, I'll return a statement called as written true. So let me try true. So true. This is how we write true in Java. And if it is not, I need to write an else block here. So if it is not, I'll return false. Okay, so this is a method. So this works now fundamentally speaking, when I if I want to test this program, what I do is I usually write inside main method, I'll be creating an object of this. So here I say new main, then with the help of this object, I'll be calling that method. Okay, so this is how I usually do it. Okay, so this is true. This method is returning. So I'll put it inside a sysout to see what value it is giving me after calling methods. So I'll take a two lines of methods one with the event number and one with the odd number. So you should find true in first and and output of second statement at line number seven should give you false as an output. So let me run this. So this is not a unit testing, you can see that here. Here I find exit two is even you have a true as an output. Here you have a three as an input and you have a right output of the method is false. But this is a manual way of testing, we cannot rely on manual way of testing for long. When we start writing enterprise, Java level applications, your code might have say hundreds of even 1000s of methods. At that time, testing is automated. So in order to automate the test in order to test each and every method of a class, we have to rely on unit testing. So let us see how I can write a unit test for this. And upon running a unit test case, how you would verify whether the function has worked or not. So let us see how to add that. So eclipse elegantly gives you a very better way of adding a unit test to it. So let's see how I do it. So for this demo, select the package explorer, I want to the package explorer window, right click on that. And after clicking that, you can you can say new, you will find a J unit test case here. If this mean is not available, go under other usually once you have created J unit test case, next time onwards, it starts happening in the same recent mean. So once again, new J unit test case. And here you mention a proper test case. So let us say it's a test case for even. So I'm writing it, you can name it properly, but make sure you always follow the naming conventions. Now what I want, I want now unit test case a few different ways of writing setting up a test case, make sure you always have a setup method checked because under the setup method, you will be creating an object. Now if you recall our earlier video, you what you find is in the main method, we have created an object of main class. And then I call that method. The same is done here in the setup method here. So I have checked the setup method and what class you are testing here. So I am testing so I'm testing the main class. So here you can write type and do you can just type main. So this is the main class which I have written in a default packet. This is the main class. Make sure you select the class. Once you select the class, then you can add a test case for that method. For example, now I'll move to next. Then after selecting a class, how many methods of the test class are put under test. Now here I want is it is is even. So I have selected is even I want a test case to be written for that is even method. So I have selected is even and I'm clicking on finish. Then it is asked me to add a library to that. Okay, so let us stick to the default implementation given to us. Then what I'm saying I want to add Java unit file library to build path. Yes, see, okay. Then it creates a test case for me. Now you can see that under default package, you have main dot Java and test case for even now let us see that by default, the test cases always fail not implemented. So now we are going to implement it. So let us comment it out. We have commented out and inside the setup. Now here, you create a reference for a main. Okay, just create a reference for a main and make sure you initialize it to null. And inside this here you will be actually initializing the object. Make sure test cases can be run many times. And whenever you run test case more than one time, setup always once executes only once. So it's very important. You write the code which only the initialize the code it has to be always written inside the setup method. So now here I'll be creating new main object. So this is new object. So I want a main object to be only created one. And here within this, I want to make sure I want to assert the method always returns to if even number is passed. So assert is one kind of test case. So now I can write here assert. So this assert you might now ask me like why how assert has appeared eclipse takes care for you in importing write packages for you. So you're not worried about what to import what not to import. So here I'm directly writing the method which will check this will call your method. Now what I want to see is whether the method returns true if even number is passed. So now what now there's a method for that we call that method is assert true. So you can rely on the autocompletion of eclipse which guides you very well. So this checks whether it whatever you want to pass between this two and brackets, will it return true or not if it returns to the test case passes if it returns false and expected is true, the test case fails. So let us write that so we have already created object we call it object dot is even and here I've written to this is what now I want to execute this so right click on to this and run as now remember you're not running applications you are now running it as a J unit test click on that. Okay, so it will allow you to ask anything let make sure everything is allowed allow access. So now you can see the result of J unit execution. It is all green it indicates that your test cases pass. Now if I if I modify this for example, if I three and what I'm expecting what I what test case I'm defining here is I'm telling that this method should return true. Okay, that's what I'm testing. But am I I am providing a wrong input three. So here in this case test case will fail if I execute this. Okay, you can see that test cases failed. So now here I'll make it is now if you want three to work. So I want you are my function to return false if it is no this is how I can assert return of false value. So if I make it as false. And now if I execute this, you will make sure you will see that the test cases passed. Okay, so this is it. Now let us see what what we have actually seen in this hands on what we are seeing is we have seen now you can just recall so we can pause the video and you can think like what we have actually learned in this video. But see what we have learned is we have seen how we can use eclipse to test a job as applications by using J unit test engine. So as a further reading what you need is you can go through this links. So you can go you can Google J unit and you can go and you can you for further reading this is it. And this was a bibliography you can go through J unit you unit guide and you can also go through the wiki to understand the fundamentals of unit testing. Okay, this is it for this video.