JUnit Tutorial | Java Unit Testing | Software Testing Tutorial | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone the sensation away from Ed Eureka and I welcome you all to this interesting session on g-unit tutorial but before we begin let's take a quick look at the agenda for today's module first of all I'll start off with what is g-unit and talk about the advantages of it and then we'll discuss about the major features of j-unit and take a look at the g-unit framework moving further to the discussion we'll take a look at how to set up J unit on your system and try to run a simple g-unit program moving ahead we'll also take a look at the J unit annotations G unit assert statements and also exceptions in J unit and then discuss about the parametrized test that takes place in J unit and finally wrap up the session by pointing out the major differences between G unit and test ng I hope the agenda was clear to you guys and I also suggest you to subscribe to a derecho channel to never miss out any updates on training technologies so let's get started what is J unit J unit is an open source unit testing framework for Java it is important in development of test-driven frameworks and is also considered as an instance of X unit architecture as the name suggests it is used for testing a small chunk of code or a unit now you might ask a question what is J unit J unit is used to verify a small chunk of code by creating a path function or a method the term unit existed even before the object-oriented era was created it is basically a natural abstraction of an object-oriented system that is the Java class or object J unit basically promotes the idea of tests first caught late so this is about J unit now let's take a quick look at the advantages of J unit J unit is one of the best testing frameworks that can be selected for an efficient testing process more application developer IDs include J unit it also provides an a WT and swing based graphical test reporting mechanism it also provides a text-based command line the open-source community has extended j-unit by implementing a giant enterprise edition test framework that enables it to test within the application service container j-unit is widely adopted by many organizations around the world for performing unit testing in Java programming language j-unit has also become a benchmark for testing in Java and is supported by almost all the IDs so this is about what does j-unit and what are its advantages now let's move ahead and take a look at the features of J unit J unit tests allows you to write codes faster which increases quality it is elegantly simple as it is less complex and takes less time to test an application it provides annotations to identify the test methods it provides test runners for running the tests these tests can be run automatically on J unit and they will check with their own results and provide immediate feedback the test cases can be run automatically on J unit and they check their own results by providing immediate feedback there is really no need to manually comb through a report of the test results okay so this is one of the major features of J unit J unit tests can also be organized into test feuds containing all the test cases this also hurts in showing test progress in a bar that is green if the test is running smoothly and it turns red when the test case fails okay so these are some of the major notable features of J unit now let's move ahead and take a look at the J unit framework J unit is a regression testing framework which is used to implement unit testing in Java to axle rate programming speed and increase the quality of code this framework also allows quick and easy generation of test data and test cases now let's discuss the major features of this J unit test framework they are namely test fixtures test suits test runners and DJ unit classes now let's get to know them in detail a fixture is a fixed state of a set of objects which is used as a baseline for running the test cases the major purpose of a test fixture is to ensure that there is a well known and fixed environment in which the tests are run so that the results are repeatable there are basically two methods under this test fixture there namely setup method and teardown method setup method is run before the test invocation the annotation at before is used in this case and in this case we use the annotation at after let's understand test fixtures with an example in case you write a code without annotations the code is not readable it is not easy to maintain and when the test suit is complex the code might contain some logical errors so these are the issues that you'll be facing if you don't use annotations while testing an application but when you compare the same code using j-unit the code is far more readable and maintainable and the code structure is way too simple and easy to understand ok so this is how test fixtures play a major role in testing an application using j-unit now let's move ahead and understand test suits if you want to execute multiple test cases in a specific order it can be done by combining all the test cases in a single origin this origin is called the test suits a test suit bundles a few unit test cases and then runs them together to run the test suit you need to annotate a class using the some annotations that is at run with a suit classes or at suit dot suit classes ok so these are the annotations that you have to do when you want to test a suit now let's talk about a test runner the test Runner is used for executing the test cases j-unit provides a tool for the execution of the test cases J unit core class is used in order to execute the tests another important method called run classes is provided by J unit Runner dot J unit which is used to run one or several test cases the return type of this method is the result object that is used to access information about the test cases you can get this result object in the following link so this is about the test Runner now let's talk about J unit classes J unit classes are used to in writing and testing J units some of the important classes are a cert test case test result okay now let's talk about assert it contains a set of assert methods we'll discuss what are these assert methods in a coming section test case it contains the test case that defines the fixture to run multiple test cases test result it contains methods in order to collect the result of executing a test case okay so this is everything you need to know about the J unit frame book now let's move ahead to our next topic and understand how to set up G unit on your system like I mentioned earlier J unit framework is used to test java-based application so before installing j unit you need to configure and verify Java development kit that is JDK in your system so the first thing you need to do is have the java libraries in your system if you don't have the Java libraries follow these steps to complete your Java installation go to the java downloads page and click on the option for java platform that is jdk in the next page do select the accept license agreement radio button accept it and click the download link against your matching system configuration now to cross-check the installation just run the following command in the command prompt it should display the installed version of java in your system as I already have Java installed in my system I'm just going to verify if it exists or not so I'm going to go to command prompt I JA v''e species - version so the version that I have in my system is 1.8 you can also download any version of your choice the latest version is 12 and this command can vary from one OS to another for Windows we use this command that is Java - version for Linux you use dollar Java - version for Mac you just have to click on tilde dollars Java version ok you can run the installer once the download is completed and do follow this connect instructions so first go to system and go to advanced system settings yeah so it pops up environment variables here just click on the environment variables and click on new and add this command so I've already added the command here so the specific location of this j-unit will be stored in my C Drive and the variable name that I'm going to provide is J unit underscore home this is a must when you're working on G unit click on OK and do note you need to add the class path accordingly so again click on new so the variable value will be percentage class path provided by the percentage as well that is a closing thing and a J unit home and slash the J unit that you're working on that is version 4 ok and the variable name would be class path click on OK click on OK and click on ok so this is how you set up J unit on your system it is just like how you setup Java on your system for the first time now once you understood how to set up J unit on your system we need an IDE in order to perform the actions I'll be considering working on the Eclipse IDE because it is user friendly and all the available jar files can be easily downloaded and eclipse and preferably if you're working on Java projects I would recommend you to use eclipse ID if you're new to J unit and you don't know how to setup eclipse on your system I'll be putting up a link in the description below just click on it and understand how to install eclipse on your system as I already have a clip I D Here I am just going to open it yeah so this is the Eclipse workspace now in order to work on j-unit first thing you need is create a new project so I'm going to go to new and go to project and I'm going to name this j-unit project and click on finish yeah you can see that a folder is being created under which the Java libraries reside and a source field where we'll write a code ok so the next thing you need to do after you create a project is to add the libraries that is j-unit libraries so I'm going to right click on my project go to build path and go to configure build path here you have to add the external j-unit jar files that you have to download so I already have the g-unit jar file so I'm going to just go to add external jars I'm going to search for g-unit yeah so I'm just going to add this and then I'm going to add a library that was j-unit library to my program how do you get this thing one way which you can get this is you can go to the Eclipse market space and search for j-unit you can install an external plugin from there ok click on next and click on finish you can see that j-unit library is added here so i'm just going to click on apply in close yeah so the j-unit jar files are added to your system so these are the following steps you need to follow in order to setup j-unit on your system now that you've understood how to setup j-unit on your system let's move ahead and learn how to write a simple j-unit program so in order to write a simple program I am going to right click on the source field go to new and create a new package I'm going to name this as Co dot at Eureka and click on finish I'm going to be writing my entire program under this package so I'm going to create a new class in order to write it I'm going to name it as g-unit class and click on finish yeah you can see that a class of the name provided that as Jane or class is created here and the first thing I'm going to do after creating the class is add the annotation test and then I'm going to be creating a new method here that is public void setup under which I'm going to print a string string I'm going to be considering as STR and I'll print this is my first g-unit program okay and then I'm going to be using the assert method in order to compare if the two sentences are equal or not so assert equals and I'm going to be writing the exact same thing that is this is my first g-unit program , specify the string so first I'll be importing the packages he is ok this assert equals yeah once this is imported I'm just going to run this so I'll just click on run here and you can verify that J unit was set up in zero seconds okay so this is how you verify if your g-unit program is successfully installed in your system or your project or not now next up I'll create another class to execute this test so I'm going to right click on my project and go to new class I'm going to name this as demo class add the main function and finish here I'm going to be writing the corresponding code in order to access the J unit class okay so first I'll create the result and create an object of result that is equal to J unit core dot run classes there will be storing in the form of a result you can already find it just click on this and the class name is the previous name of the class that is the J unit class okay I hope you're clear with this it'll ask us to import here so you have three options here for import check which one is suitable the using j-unit runners will be importing the g-unit runner packages so here as well let's see what it throws an error here it is asking us to create a new class or interface okay let me see what mistake I've done here yeah so the name is J unit and I've used uppercase for you J unit is case sensetive and class okay and I'm going to be checking for a condition for failure creating an object of the same failure and check the result of get failures okay so it returns the results in the form of a list okay so I will just import this failure packages yeah done and I'm going to be writing the corresponding code inside this so I'm going to consider printing it first system dot out dot println in this I'm going to be printing failure dot - string okay so this basically helps in printing the results which are filled and after this I'm going to print another statement not a system dot out dot println I'm going to be writing result is equal to and specify the result location that is result dot was successful okay so this prints the test case which was successful now let's try running this program save it and run yeah so it prints the result is true now how is the result true it checks for the condition for failure the result is get failures and if this condition satisfies it is going to print the failure string that is the failed string in case if it is true it prints this particular command that as result is equal to so this is how you test an application or test a program using j-unit now that you've learned how to write a simple g-unit program let's understand the test ng annotations J unit framework is built on annotations an annotation is a special form of syntactic metadata that can be added to a Java source code for better code readability these annotations provide the information regarding the methods that are going to be run before and after the test methods the methods that are run before and after all the methods are completed the methods or classes that will be ignored during the test execution okay so these are the following list of annotations that we have that is test before after before class after class and ignore now let's understand them in DD annotations start with a letter at okay so this test annotation tells j-unit that the public void method can be run as a test case now let's talk about before this helps in annotating a public void method with annotation before which causes the method to be run before each test method okay so this method is called before you execute any test case what about after if you allocate external resources in a before method you need to release them after the test runs so one way to do that is annotate the method with at after which causes that method to be run after the execution of the test method talking about before class this method helps in annotating a public static void main with at before class which causes it to be run once before any of the best methods in the classes are run talking about after class this will perform the method after all the test cases are run this is used to perform cleanup activities as well talking about the ignore annotation this annotation is used to ignore the test that will not be executed this was also used in order to skip the test cases okay so these are the annotations that you need to be perfect when you're working with j-unit now let's move ahead and understand what are the assert statements that are available in j-unit assert is a method which is used in determining pass or fail status of a test case in je unit all the assertions are in the assert class so this class basically provides a set of assertion methods which are useful for writing the test cases do note that only failed assertions are recorded so there are different methods under this assert command we have eight assert statements in total let's take a look at them one by one the first method is void assert equals which is of the form boolean expected and boolean action so this basically checks if the two primitives or objects are equal okay next we have void assert true which takes in the condition boolean true or false so this basically checks whether the condition is true next we have assert false which also takes in the boolean condition and it checks whether the condition is false next we have assert not null which checks whether an object isn't null void a certain all which checks whether an object is null okay you note that not null is used to check whether an object isn't null to check whether an object is null you use a certain Ali okay which takes in the object next up we have a search same which compares two of the references so this assert same method tests if two object references point to the same object assert not same checks whether the two object references do not point to the same object or not the last we have assert RA equal so this is similar to assert equals just that you'll check to erase instead of to object okay so this a third array equals method we'll test whether two arrays are equal to each other or not let's take a quick look at the example for this so I'll create another class under this package a third click on finish yes under this I'm going to be writing the corresponding commands the first thing I'll do is create an annotation of test and our public void test assertion yeah under which I'm going to test the data so the first one will be of the form string SDR equals so I'm going to create a new string new string and give this a text I'm going to be giving ed Eureka or ed you yeah this is fine I'm going to provide another string and name it STR one so that you can differentiate the two strings I'm going to create another string give this as the same ed you now once you've provided the input for the first two strings let's consider the next strings so the next string which I would like to write would be so I'm going to instantiate the string command to null okay and then I'm going to create another string then instantiate it to the value that is specified here that is edu okay so two of them have the same values but one string would be considered as null I'm going to create another string STR for even this will be a cue and the next thing I'll do is provide two integer numbers in order to create an RA so I'm going to be writing int value is equal to five I'm going to be giving it as five next and give another value value one or two I'm going to be naming it of one six after you input the values which is of the form integer I'm going to be creating an array of the form string so string array and I'm going to create an object of this that is expected array is equal to in the flower braces I'm going to be writing 1 comma 2 comma 3 okay so this is the expected array which I want to compare it with and the result array would be the same okay so I'm just going to copy this thing and paste it over here okay so that the expected array and the result array are the same now let's check if the two objects are equal so I'm going to be using the assert method in this case so I'm going to write assert equals string and string one yeah and it throws up another it sees create a method let me just fix this first import the test ng libraries yeah and assertequals it'll state ID static import g-unit methods done and once you've done checking if the two objects are equal let's check for another condition for the true condition so check for true and I'm going to be writing assert true and I'm going to be providing value less than value one so this basically checks whether the condition is true or not so I'm going to import the static true and after you've done checking for the true condition let's try to check the false condition as well check for false that is assert false and I'm going to provide the same condition that is value less than value one well import this as well now once you're done checking if the condition is true or false and also if it is equal or not let's check for the null condition check for null so assert not null will have the string now let's check if it is null so assert null I'm going to be taking the second string which I've mentioned here as null you can see that I've declared this as null so I'm going to be considering writing string two and import this as well so once you've done this let's also check if two objects this point to the same object okay so we've already come across this assert statement that is assert same so I'm going to write a comment here that is check if the reference is to the same object okay I'm going to write a cert same and I'm going to be comparing string three and string 4 I'm going to import the assert same packages done so now let's try to run this program so it says the assertion took place in point to zero seconds okay now let's try writing the main function in order to run this so I'm going to create a new class and call it main class include the public static void mean and finish I'm going to be writing the code here so now I've called this class as main class and I'm going to be writing result create an object of the same is equal to J unit Corps dot class that is the name of the class that we want to search for that has run classes so the name of the class that we had before was assort a third class so a third dot class done so we need to import the result packages do note that you import g-unit ronald packages and after this i'm going to be considering the same for loop here for the condition failure create an object of it that is failure is the result of get failures which returns it in the form of a list i'm going to be importing the failure functions I'm going to create this and print failure dot to string so this helps in printing the failed string or I'm going to be printing the result dot was successful so this will print the test case which was successfully executed he saved this and run this program so the output of this is the test assertion dot assert is null okay now let's take a look at our assert program here when we've provided this particular command it says ascertain all string two so this condition is true so it'll be printing null now let's run this program it says test assertions code odd and Eureka dot assert is null and the result is false so it is printing that the test case has failed so or else it would have printed that the test result was successful and it would have printed that the result is true now let's take a look at our assert program so we've provided the first and the second string to be equal so first thing it checks for equal command is string and string 1 so this basically checks between the two strings that a string and string 1 if they are equal or not they're equal you can see that it is equal and it also checks for assert true function where we've provided two values and checks if the condition is true so the value is 5 5 is less than 6 so it is true for false value is less than value 1 so this does not print false because value is less than value 1 that is 5 is less than 6 so this is true ok so this will not print false now talking about not null string we've provided the value of the string which has the string edu this is not null so this will return true talking about a certain 'l it has a value of string to which we've provided to be null so even this will return true at the same time we are going to be comparing the same function that was asserts in between the two strings 3 & 4 wherein they possess the same string which is add you now talking about the main class you can see that there is printing false ok so that means your test case has failed so let's analyze how you can solve this problem let's go to our assert function and take off all these static equals method ok the import methods that we've hired which is of the form static let's take all of them out and import the package which says j-unit assert this is actually way too simple because you have all the assert statement commands inside this so I'm going to just import this done and then add a few commands here so I'll be writing another command to check if it is not same or not so assert not same string one that a string comma string one so this basically checks if string and string one are same or not next we have to check the array so assert array equals so we were basically checking for the arrays here so the name we provided here was expected array and the result array so the expected array would be expected array and the result array okay so this is done so I'm just going to save this and run my main class now you see if the result is true or false it says false again now it is printing Falls again this is because that a condition that are specified here for false is not true okay so this says value is less than value one which means it is true so we have to provide a condition which says it is not true so it is greater than value month that is five greater than six will throw up an error which say is false now let's save it and run the main function it will print true so this is how assert statements in g-unit works now let's move ahead and understand what are the exceptions are occurred during the execution of a program J owner provides an option of tracing the exceptions that occur in the code you can test whether the code draws the desire exception or not and the expected parameter is used along with the test annotation okay so while testing for an exception you need to ensure that the exception class that you're providing in that optional parameter of the test annotation is the same this is because you are expecting an exception from the method that you're testing otherwise the J unit test would fail okay for example at tests expected illegal argument exception or class so this is an exception so by using the expected parameter you can specify the exception name that your test May through so you will get this illegal argument exception which will be thrown by the test case if a developer tries to use an argument which is not permitted okay so this is one of the notable exceptions that occur during the execution of a process in J unit now let's take a look at the parameterize test in J unit J unit four has introduced a new feature called parameter ice test this test allows the developer to run the same test over and over again using different values so you have a few steps in order to create the parameter I steps let's take a look at how it is done so you need to first annotate the test with run with this mainly specifies that it was a parameter ice test then create a public static method annotated with parameters that returns a collection of objects in the form of an array as test data set moving ahead create a public constructor that takes in what is equivalent to one row of the test data create an instance variable for each and every column of test data and also create your test cases using the instance variable as the source of the test data okay so that's it about parameterize test now let's take a look at the final topic that is j-unit vs. test ng test in G and J unit both are well known terminal cheese when you talk about software testing frameworks test in G is almost similar to G unit except for certain functionalities so I will be comparing J unit and test in G based on their features so the major features I'll be considering to compare J unit and test in gr the suit test annotation support ignore test execution before all the test cases in a suit dependency test and parametrize tests so let's compare them based on this u test J unit and test in G both consists of the suit test that is the annotation suit okay and it also possesses the annotation support through both the J unit and test and G ignore test in the case of J unit is specified using the command at ignore whereas in testan g you use a command called artist enable false okay so in testing G you specify if the enable is true or false okay not talking about the execution before and after all the test cases in the suit J unit does not execute before and after the suit whereas test and she provides commands like before suit after suit before test and after test talking about the dependency test J unit does not support dependency test whereas using test in G you can perform dependency tests now talking about the parameterize test parameters test in J unit can be done using the annotation run with and parameter whereas in tests in G the parameterize tests can be carried out using the annotation data provider okay so this is everything you need to know about J unit if you are a beginner so with this we come to the end of this session on g-unit tutorial I hope you guys have enjoyed this video if you want to learn more about software testing its methodologies its types and many more don't forget to subscribe to our channel and click the bell icon for notifications on latest technologies that's it from my end for today guys if you have any queries regarding this topic don't forget to comment your queries on the comment section below thank you for watching this video happy learning I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any Rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 175,610
Rating: undefined out of 5
Keywords: yt:cc=on, junit tutorial, junit edureka, junit, junit tutorial edureka, junit tutorial for beginners, junit tutorial - java unit testing with eclipse, junit complete tutorial, What is JUnit? advantages and uses, features, the junit framework, how to set up junit, how to run a simple junit program, junit annotations, junit assert statements, junit exceptions, parameterized test, junit vs testng, software testing edureka, edureka, edureka learning
Id: SDwqcFwvwY0
Channel Id: undefined
Length: 38min 16sec (2296 seconds)
Published: Thu Nov 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.