Asiri Rajapakshage

Jun 2, 2020

3 min read

Junit

If you testing your project after it deployed , you would have big big bugs to correct. The goal of a software tester is to find bugs, find them as early as possible, and make sure they get fixed. If you are building the entire application and deployed it and then test the application is very difficult and cost of bugs will increase . It is called as screen testing (or system testing) . So i think you can understand the advantage of doing small unit or component testing during the development.

What is Unit testing ?

Unit testing is testing a specific class, method (individual unit or component) of a software during the development time by software developers. When a problem is found at the unit level, the problem must be in that unit . Then It is very easy to fixed that problem . There are few automated tools that is available for doing unit testing .

1 . Junit

2 . JMockit

3 . Nunit

4 . EMMA and … etc .

In here we Only talk about Junit .

What is JUnit ?

Junit is a framework which are help developers to testing the method and check whether that output as expected . It is a free to use testing tool used for Java programming language. It provides assertions to identify test method. This tool test data first and then inserted in the piece of code.

Now we look How we can use Junit ? ( with some Example. )

EX :-Go to user Java IDE and create Java project . Then within src folder create package with whatever name you like ( ex :- i will use com.asiri.junit ). within that package create Calculation class ( as below) with addition, subtraction, division and multiplication methods.

Now we will see how to create Test class .

Within your project create test folder . within that test folder create Package with name you used earlier ( ex :- com.asiri.junit ) . ( If you use IDE there is some sort way to do this things . In IntelliJ click on class name of Calculation class , then you will see bulb icon . click on that bulb icon and select Create Test ).

  • Create a java test class, say, CalculationTest.java.
  • Add a test methods sum(), sub(), div() and mul() to your test class.
  • Add an Annotaion @Test to all method that we write.
  • Implement the test condition and check the condition using assertEquals API of JUnit.

Finally you can run and test the each method separately .

And a developer writes a section of code in the application just to test the function. They would later comment out and finally remove the test code when the application is deployed. you can learn more about Unit testing from here .

Thank you .