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 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
Post a Comment