Tuesday, 7 July 2015

JUnit






Unit Testing

Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
A unit is the smallest testable part of an application(method or class).
A unit test is a piece of code written by a developer that executes a specific functionality in the code under test.

Benefits

Identifies defects early in the development cycle.
Makes sure that further changes do not introduce problems into previously correct code.
Testing forces the programmers to read and analyze their code, thus removing defects through constant code verification.

Terminology

Test Fixture: sets up the data (both objects and primitives) that are needed to run tests
Unit Test :  test of a single class / Method.
Test Case: tests the response of a single method to a particular set of inputs
Test Suite: collection of test cases
Test Runner :  program/ software that runs tests and reports results

JUnit is an open source Java testing framework used to write and run repeatable tests.
JUnit helps the programmer to define and execute  test case and test suits.
JUnit library is down-loadable from http://www.junit.org/
Junit1.4.x supports Annotations (@Test ..)
Most of the Java IDE supports JUnit.

To define a Junit test case:
implement a subclass of TestCase(optional in Junit1.4)
define instance variables that store the state of the fixture
define methods that implement individual tests(or use @Test annotation)
initialize the fixture state by overriding setUp()(or use @Before annotation)
clean-up after a test by overriding tearDown().(or use @After annotation)

Lifecycle

The lifecycle of a TestCase used by the JUnit framework is as follows:
Execute  setUp()method or method annoted with @Before annotation.
Call a test-prefixed method or method annoted with @Test annotation
Execute  tearDown() method or method annoted with @After annotation
Repeat these steps for each test method.



Annotations
@Test
@RunWith(Sequencer.class)
@Sequence(priority = 1)
@Ignore

import junit.framework.TestCase;
public class Test extends TestCase {
/*fixtures */
protected double fValue1;
protected double fValue2;
/* This method is called before a test is executed */
protected void setUp() {
fValue1 = 2.0;
fValue2 = 3.0;
}
/* This method is called after a test is executed */
protected void tearDown() {
fValue1 = 0.0;
fValue2 = 0.0;
}


/* Implement one or more no-argument void methods prefixed by the word test*/

public void testMin() {
double result = Math.min(fValue1, fValue2);
assertTrue(result == 2.0);
}

public void testMax() {
double result = Math.max(fValue1, fValue2);
assertTrue(result == 5.0);
}
}



public class EmpTest extends Assert {

public static SimpleDateFormat sdf;
private Employee emp;

@BeforeClass
public static void BeforeClass() {
sdf = new SimpleDateFormat("yyyy-mm-dd");
}

@Before
public void setUp() throws Exception {
Date date = sdf.parse("2011-11-11");
emp = new Employee("james", date);
}


@Test
public void getName() {
assertEquals(emp.getName(), "Jammy");
}

@Test
public void getAge() {
assertEquals(emp.getAge(), 35);
}

@After
public void tearDown() throws Exception {
emp = null;
}

@AfterClass
public static void AfterClass() {
sdf = null;
}
}


A TestSuite is a Composite of Tests. It runs a collection of test cases.

        public class AllTests extends TestSuite {
             static public Test suite() {
                 TestSuite suite = new TestSuite();
                 suite.addTestSuite(TestMath.class);
                 suite.addTestSuite(SimpleTest.class);
                 return suite;
                  }
           }





No comments:

Post a Comment