Skip to main content

JUnit in net beans


Installation of JUnit in net beans
 If you are using net beans 8.0 than JUnit plugin is already installed in net beans otherwise you have to install it by following method.
Open net beans click on tools menu on the menu bar .click on plugins.
Plugins window will appear. In the search box write JUnit. It will appear in search result. Click on JUnit than click install. It will be automatically downloaded and installed.
Basic Usage
1)    Create a class
Create a class to be tested. We added a function of sum in it which will be tested.
*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javatest;

/**
 *
 * @author Cyber Tech
 */
public class Javatest {
    public int add(String num1,String num2)
    {
        int number1=Integer.parseInt(num1);
        int number2=Integer.parseInt(num2);
        return number1+number2;
    }

  
   
}

2)    Create test Class
1        On menu bar click on tools
2        Click on create /update tests
3        The following window will open
4        Click on ok the test class will be created.
package javatest;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Cyber Tech
 */
public class JavatestTest {
   
    public JavatestTest() {
    }
   
    @BeforeClass
    public static void setUpClass() {
    }
   
    @AfterClass
    public static void tearDownClass() {
    }
   
    @Before
    public void setUp() {
    }
   
    @After
    public void tearDown() {
    }

    /**
     * Test of add method, of class Javatest.
     */
    @Test
    public void testAdd() {
        System.out.println("add");
        String num1 = "";
        String num2 = "";
        Javatest instance = new Javatest();
        int expResult = 0;
        int result = instance.add(num1, num2);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
   
}


This is the test class for our function number1 and number2 are inputs and expected result is what will be the output of the function we expect.

@Test annotation tells JUnit that following method is a test method. We write input in number1 and number2 for example we give num1=”2”, number2=”2” and expresult=4 than we right click on our test class and click run test. The test will  run and Assert Equal () function will compare the expresult and output and pass test if both are same otherwise it fails tests.
Following is the result of above test.


Test suite
We have to do multiple tests on a single unit. It is great problem to run them separately so we combine them and run them together. Combination of test cases is called test suites.we do it by making another test function  it is demonstrated below in code
Following is the image of a test suite runned.
public JavatestTest() {
    }
   
    @BeforeClass
    public static void setUpClass() {
    }
   
    @AfterClass
    public static void tearDownClass() {
    }
   
    @Before
    public void setUp() {
    }
   
    @After
    public void tearDown() {
    }

    /**
     * Test of add method, of class Javatest.
     */
    @Test
    public void testAdd() {
        System.out.println("add");
        String num1 = "2";
        String num2 = "3";
        Javatest instance = new Javatest();
        int expResult = 5;
        int result = instance.add(num1, num2);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
    @Test
    public void testAdd1() {
        System.out.println("add");

        String num1 = "3";
        String num2 = "4";
        Javatest instance = new Javatest();
        int expResult = 8;
        int result = instance.add(num1, num2);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
       
    }
   
}
In the above code we have added @test annotation at the top of testAdd1() function to tell compiler that it is a test function . Testadd1 which we have created now will not pass because expResult and output are different and we will get 50% result on running this test. Result is given below. 

 

 JUNIT Plug-ins for other languages:
JUnit has been ported to other languages and following plugins are used for testing other languages  which are following
1.     PHPUnit
           PHPUnit is a unit testing tool for the PHP programming.
2.     NUnit
            It is made for unit testing of c# which is a part of .NET.

3.     PyUnit
             It is made for testing of python programing knowledge.

4.     fUnit
               It is made for testing of fortan programing knowledge.

5.     Test::Class and Test::Unit
              It is made for testing of perl programing knowledge.
6.     CPPUnit
             It is made for testing of C++ programing knowledge.

 

We will discus more on junit in upcoming posts  stay connected and stay updated!

Comments

Popular posts from this blog

Pointers in C

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them. What are pointers? Why should you care? Pointers are aptly name: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that you ca...

Types of Computer Networks(LAN,MAN,WAN)

Types of Computer Networks Computer networks are categorized according to: How they are organized physically. The way they are used. The distance over which they operate. Three main types of computer networks are as follows: LAN   (Local Area Network) WAN (Wide Area Network) MAN ( Metropolitan-Area Network) LAN (Local Area Network) LAN is the most common type of network . LAN stands for Local area Network . It covers a small area. Most LANs are used to connect computers in a single building or group of buildings. Hundreds or thousands of computer maybe connected through LAN. Typical LANs can be found in industrial plants, office buildings, and college or university campuses. LANs are capable of transmitting data at very fast rate. Data transmission speeds of LAN are 1 to 100 megabits per second. It is much faster than data transmission over a telephone line. LAN can transmit data in a limited distance. There is also a limit on the number of...

Ring topologies

Ring topologies are similar to bus topologies, except they transmit in one direction only from station to station. Typically, a ring architecture will use separate physical ports and wires for transmit and receive. Token Ring is one example of a network technology that uses a ring topology.