Junit: Introduction To Testing Java Programs With Junit
Junit: Introduction To Testing Java Programs With Junit
Junit: Introduction To Testing Java Programs With Junit
Further documentation is also available from the same source. I suggest reading the FAQ after
you have read this introduction, then moving on to the other documents found there for
descriptions of more sophisticated uses of JUnit.
JUnit should be in the default classpath on all of the CS lab computers, both Windows and Unix
systems, so you shouldn't need to do anything special to use JUnit when developing your
programs there.
Installation
Below are the installation steps for installing JUnit:
Important: don't install junit-4.6.jar into the extension directory of your JDK installation. If you
do so the test class on the files system will not be found.
11
Creating a test class
Suppose you are working on a class called MathUtil, mainly because Java doesn't provide a
function to calculate base 2 logarithms. In order to use JUnit to test your methods, you will need
to create a separate class, which we'll call MathUtilTest. There are three things you must do as
you set up this class in order to run your tests:
1. Import junit.framework.*.
2. Extend TestCase.
3. Provide a constructor with a single String parameter. This constructor should call the
TestCase constructor with the parameter.
Here is a minimal class that demonstrates the structure of classes derived from TestCase:
import junit.framework.*;
Note that if you don't supply a constructor of the form indicated here, you'll get error messages
from javac complaining that TestCase( ) is not public in junit.framework.TestCase. The authors
of JUnit did this to you on purpose to force you to write a constructor for your TestCase-derived
class.
12
command line and one GUI-based. We'll focus on the GUI test runner; see the JUnit
documentation for details on the character-based driver.
The GUI test driver is called junit.swingui.TestRunner. It allows you to specify the test class to
run, then when the "Run" button is clicked, it examines the class you've chosen and finds all
methods whose names begin with "test." (To get an idea of how JUnit does this, have a look at
the documentation for java.lang.Class and find out about "reflection.") It runs each of these
methods, updating a progress bar based on how many tests have executed successfully.
If one of your tests fails, you will see a description of where the failure occurred, including line
numbers for the method calls that were active at the time of the failure and any exceptions that
were thrown.
Many IDEs will work with JUnit. For example, NetBeans, an open source Java IDE, has built-in
support for JUnit. It will generate a skeleton TestCase class from your class definition, and the
error reporting is tied in with the included program editor.
If you are running from a command prompt, type a command that looks like this:
On Unix, you will change directories to your source directory and execute a command that looks
like this:
where the name of your test class is substituted for MathUtilTest. This works just the way it
would from a Windows command prompt.
Writing tests
Writing tests for JUnit involves using the methods of the junit.framework.assert class. This is a
reasonably small class, and you should read the javadoc material about it to get a complete view
of its capabilities. Here we'll just consider a few representative methods. Each of these methods
checks some condition and reports back to the test runner whether the test failed or succeeded.
The test runner uses the result to update the display. All of the methods return void (i.e. they are
procedures).
13
assertTrue(boolean)
You can supply any boolean expression to this method, and as long as the boolean expression
evaluates to true, the method reports success. If the condition you want to check should be false,
simply prepend the "!" negation operator to your parenthesized expression.
assertTrue(String, boolean)
Same as the first form of assertTrue, except that the supplied String is printed if the assertion
fails. Most methods have a version of this form with a message string.
assertEquals(Object, Object)
Compares the two objects you pass in using the equals( ) method.
assertNull(Object)
Succeeds if the Object reference passed in is null. There is also an assertNotNull method.
fail(String)
Program 1 :
Suppose we have written an absoluteValue method for Integer objects in our MathUtil class.
Here is a JUnit test for the method.
14
Classes and result of junit testing these are done in netbean IDE
int a=5;
int b=6;
int sum(){
int d= a+b;
return d;
int c;
c=a1.sum();
System.out.println("sum is"+c);
Junit test
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
15
import org.junit.Test;
/**
* @author Administrator
*/
public addTest() {
@BeforeClass
@AfterClass
@Before
@After
16
public void tearDown() {
/**
*/
@Test
System.out.println("sum");
int expResult = 0;
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
/**
*/
@Test
System.out.println("main");
String[] s = null;
add.main(s);
// TODO review the generated test code and remove the default call to fail.
17
}
Program 2 :
import java.awt.*;
import java.applet.*;
//>>>>>as there is use of panels so will declare accoring to panel in which they come
18
Label leid = new Label("EMail ID");
p1.setLayout(new GridLayout(6,2));
p1.add(lname);
19
p1.add(tname);
p1.add(ladd);
p1.add(tadd);
p1.add(ltno);
p1.add(ttno);
p1.add(leid);
p1.add(teid);
p1.add(ldob);
p1.add(tdob);
p1.add(lage);
p1.add(tage);
add(p1);
p2.setLayout(new GridLayout(1,3));
p2.add(lsex);
p2.add(cmale);
p2.add(cfemale);
add(p2);
20
p3.setLayout(new GridLayout(2,2));
p3.add(ledu);
p3.add(tedu);
p3.add(lex);
p3.add(tex);
add(p3);
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author Administrator
*/
21
public Exercise2Test() {
@BeforeClass
@AfterClass
@Before
@After
/**
*/
@Test
System.out.println("init");
22
Exercise2 instance = new Exercise2();
instance.init();
// TODO review the generated test code and remove the default call to fail.
Program no. 3
/*
*/
/**
@author ss
*/
int c;
c = a + b;
return (c);
void prnt()
23
}
c1.add(5,10);
c1.prnt();
TestTool :
/*
24
* and open the template in the editor.
*/
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author ss
*/
public chechTest() {
@BeforeClass
@AfterClass
25
@Before
@After
/**
*/
@Test
System.out.println("add");
int a = 0;
int b = 0;
int expResult = 0;
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
/**
26
*/
@Test
System.out.println("prnt");
instance.prnt();
// TODO review the generated test code and remove the default call to fail.
/**
*/
@Test
System.out.println("main");
chech.main(args);
// TODO review the generated test code and remove the default call to fail.
27
28