Unit Testing in Eclipse for Java #Java Short Notes

Unit Testing in Eclipse for Java: just an overview

Why Unit Test?

Say, you have written some Java classes, before making use of them in real application functionalities, it's always better to check whether the classes (their functions/methods) are working properly. Unit tests simply test a single unit like a single class, or a single function at a time. It's almost always a good practice to design the software first, also design the classes and their methods along with interactions among classes, early in the project. After, classes and their methods are written, you should do some testing to make sure they are working properly.

How to Unit test in Eclipse?

1. You need to add junit.jar in your project build path. You can do that by selecting the project, then right click, then select properties, the java build path, then libraries tab, and then add jars/add external jars and select JUnit.jar.

2. You can create a folder to keep your unit test codes say unittestcodes.

3. The basic idea is, to test a class, you will need to create a test case class with methods like setUp, tearDown, and test methods for all the methods that you want to test (one for one). The setUp() method can be used to create the data for testing [you have to write the code though]. The other test methods will call the corresponding class methods using the data to check if the methods are working properly or not. Methods like assertTrue may come handy in the test methods.

4. How to create these test classes and test methods?

Right click the folder for testcode like unittestcodes, select new, select other, type Junit in the text box, select Junit test case...and then provide other parameters like the name of your test class, which class do you want to test, do you want to have setUp(), tearDown() methods or not?. In the next step, you have to select which methods of the class you want to test. Then the test class (skeleton) will be written for you.

5. In the setUp() method, write code to build test data. From the other test methods call the related class methods to test them.

6. How to run the experiment: right click the name of this test class from the left window, select run as, select Junit test case. Now analyze the output and fix the class methods if required. Try with different data, data from different ranges, boundary value data and you know what to test in your use cases. From: http://sitestree.com/?p=4821
Categories:Java Short Notes
Tags:
Post Data:2007-03-28 00:33:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Leave a Reply