Junit

JUNIT Projects

JUNIT Project 1

JUNIT Examples

JUNIT EXAMPLE

adplus-dvertising
How does JUnit working for Java Program
Previous Home Next

JUnit is a Library package.In this library file in jar file inbuilt. You can define in other word thing it contains a tool (called test runner) to run your test files.

Basically it is not an automated testing tool, it means it's not working for automated, it's work for manually to write your test files by hand. JUnit does give you some support so that you can write those test file conveniently.

Suppose you want test your Java program. and in your program you have class A that you want to test this class. Then we'll write the tests in a new class, when you want to test this class then time this class call it Atest. This Atest is our test file. We will typically group the tests in bunch of methods called test methods.

To actually test A we need to executing test class Atest.This is done by calling Junit's test runner tool; then we can pass the name Atest to it. The JUnit framework will execute Atest for your program.

JUnit report how many of the test methods in Atest succeed, and how many failure. The detail of each failure will be reported.

Note: If you don't have JUnit, then you need to downloaded it. To use it you just need the jar file. A full download also contains it's documentation. The using of jar file to test the program. this jar file name is junit-version-number.jar. If you use the JUnit then you will need to add a full path to this jar to your class path.

The First Simple Example:

A simple program generated and after that we want to test this class. but firstly we explained a bit about what it is.The class is called Subscription an instance of it represented by a subscription to something. like as example newspaper, but it does not really matter here.

package R4R;
public class Calculation {
public static Class java;
int add( int i, int j)
{
return i+j;
}
int sub(int i,int j)
{
return i-j;
}
int mul(int i, int j)
{
return i*j;
}
int divide(int i, int j)
{
return i/j;
}
}
And you test this program for JUnit.
package R4R;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class CalculationTest extends TestCase 
{
private Calculation calcR4R;
protected void setUp() throws Exception 
{
super.setUp();
}
protected void tearDown() throws Exception 
{
super.tearDown();
}
public CalculationTest(String arg[]) 
{
super(arg[0]);
calcR4R = new Calculation();
}
// test case method name should start with test.
public final void testAdd() {
assertEquals(calcR4R.add(20, 30), 50);
}
// test case method name should start with test.
public final void testSubtract() {
assertEquals(calcR4R.sub(60, 30), 30);
}
// test case method name should start with test.
public final void testMultiply() {
assertEquals(calcR4R.mul(100, 3), 300);
}
// test case method name should start with test.
public final void testDivide() {
assertEquals(calcR4R.divide(333, 3), 111);
try {
assertEquals(calcR4R.divide(100, 0), 20);
} catch (Exception e) {
// Exception is expected so asserting for true.
assertTrue(true);
}
}
//Sample JUnit code to run in text mode (CLI – command line).
//Add this code to the CalculationTest.java to run in CLI.
public static void main(String[] args) {
junit.textui.TestRunner.run(CalculationTest.suite());
}
private static TestSuite suite() {
TestSuite suite = new TestSuite("Test for R4R.Calculation.java");
//$JUnit-BEGIN$
suite.addTestSuite(CalculationTest.class);
//$JUnit-END$
return suite;
}
}
Previous Home Next