White Box Testing
Test your code
   Home      UnitTestingWithJUnit3
 

This is a brief tutorial on JUnit 3. The goal is to introduce basics of JUnit 3 framework.
 
Divide your testing in Test method, Test classes and Test suite. And write test framework in bottom up approach.
 
Write test method for each method for every class file to be tested.Method name must begin with "test" and should not throw exception.
 
Write test classes for every class to be tested.
An import statement to bring in junit.framework.* and extends test classes  from TestCase .Call constructor for the test classes with a call to super(string).
                     
In order to run each test independently and do the common resource association for all the tests of a class, method setUp() will be called before each one of the test method executed, and to release the resources acquired in setUp() the method tearDown() will be called after each test method get executed
 
protected void setUp();
 
protected void tearDown();
 
To run a GUI version that lets you pick and choose classes (and which remembers them from session to session), run the following class:
 
java junit.swingui.TestRunner
 
You'll probably be able to run the junit.swingui.TestRunner class from your IDE. If not, run it from the command line using the jre or java command (as shown).
 
java junit.textui.TestRunner classname ...
 
import junit.framework.TestCase;
 
public class CalculatorTest extends TestCase {
 
           
            public CalculatorTest(String name) {
                        super(name);
            }
 
            
            protected void setUp() throws Exception {
                        super.setUp();
            }
 
          
            protected void tearDown() throws Exception {
                        super.tearDown();
            }
 
          
            public void testAdd() {
                        Calculator cal = new Calculator();
                        long result = cal.add(3, 4);
                        assertEquals("actual not matching with expected", result, 7);
            }
 
           
            public void testSubtract() {
                        Calculator cal = new Calculator();
                        long result = cal.subtract(10, 5);
                        assertEquals("actual not matching with expected", result, 5);
            }
 
           
            public void testMultiply() {
                        Calculator cal = new Calculator();
                        long result = cal.multiply(10, 5);
                        assertEquals("actual not matching with expected", result, 50);
            }
 
           
            public void testDivide() {
                        Calculator cal = new Calculator();
                        long result = cal.divide(10, 5);
                        assertEquals("actual not matching with expected", result, 2);
            }
 
}
 
Test suite helps user in grouping multiple suites or user want to execute some selected tests within a suite
 
An import statement to bring in junit.framework.*
 
Create suite() method which instantiate and returns TestSuite class object. Give the name to instantiated TestSuite object to identify it later.Call addTestSuite method of instantiated TestSuite object to add your TestCase or TestSuite object
 
import junit.framework.Test;
import junit.framework.TestSuite;
 
public class AllTests {
            public static Test suite() {
                        TestSuite suite;
                       suite = new TestSuite("Test Main");
                       
                        try{
                                    suite.addTestSuite(CalculatorTest.class);
                                    suite.addTestSuite(OtherTests1.class);
                                    suite.addTestSuite(OtherTests2.suite())
                                               . . .
                                               . . .
                        }  catch(Exception e) {
                                    System.out.println( e );
                        }
                       
                       return suite;
            }
}
 
 
You'll probably be able to run it from your IDE. To run it from the command line using the jre or java command (as shown).
 
java junit.textui.TestRunner classname ...
 
TestRunner expects the name of a TestCase class as argument. If this class defines a static suite method it will be invoked and the returned test is run. Otherwise all the methods starting with "test" having no arguments are run.
 
Put the test case statement within try catch block
 
when an expected exception is thrown call assertTrue(true) within the catch block
 
when an unexpected exception is thrownCall Fail(msg) within the catch block or Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error
 
To determine whether a method under test is performing correctly or not we use assertions. They tell you that some condition is true, equal, or not, and so on.
 
All methods will record failures or errors, and report these through the JUnit classes.
 
assertEquals:Assert two objects or primitive data type equal
assertEquals(String message, expected, actual)
assertEquals(String message, expected, actual, tolerance)
message: An optional field that will be reported in the event of a failure or error.
expected: value that is to be expected after running test
actual: value actually produced by the code under test
tolerance: An optional field that discard decimal digit for floating point numbers
 
assertNull:Assert that an object is null
assertNull(String message, java.lang.Object object)
assertNotNull:Assert that an object is not null
assertNotNull(String message, java.lang.Object object)
message: An optional field that will be reported in the event of a failure or error.
object: To test that the given object is null (or not null).
 
assertSame:Assert two object refer to same object
assertSame(String message, expected, actual)
assertNotSame:Assert two object do not refer to same object
assertNotSame(String message, expected, actual)
message: An optional field that will be reported in the event of a failure or error.
expected: the object that is expected to be same or not with actual object after running test
actual: the object actually produced by the code under test
 
assertTrue:Assert the condition is true
assertTrue(String message, boolean condition)
assertFalse:Assert the condition is false
assertFalse(String message, boolean condition)
message: An optional field that will be reported in the event of a failure or error.
condition:The conditional statement tested to be true or false
 
Fail:This method should be called for unexpected error
fail(String message)
message: An optional field that will be reported in the event of a failure or error.
 
 
 
 
http://www.junit.org/