Testing is the process of checking the functionality of the application whether it is working as per requirements and to ensure that at developer level, unit testing comes into picture. Unit testing is the testing of single entity (class or method). Unit testing is very essential to every software company to give a quality product to their customers.

A unit test is a piece of code written by a developer that executes a specific functionality in the code to be tested. The percentage of code which is tested by unit tests is typically called test coverage.

 

A unit test examines the behavior of a distinct unit of work. Within a Java application, the “distinct unit of work” is often (but not always) a single method. By contrast, integration tests and acceptance tests examine how various components interact.

 

A unit of work is a task that isn’t directly dependent on the completion of any other task.

 

JUnit is a framework that was started by Kent Beck and Erich Gamma.

Erich Gamma is one of the Gang of Four who gave us the now-classic Design Patterns book

A framework is a semi-complete application. A framework provides a reusable, common structure to share among applications. Developers incorporate the framework into their own application and extend it to meet their specific needs.

 

JUnit (http://www.junit.org) is open source software, released under IBM’s Common Public License Version 1.0 and hosted on SourceForge. The Common Public License is business friendly: people can distribute JUnit with commercial products without a lot of red tape or restrictions.

Unit testing framework should follow several best practices like

– Each unit should run independently of all other unit tests.

– The framework should detect and report errors test by test.

– It should be easy to define which unit tests will run.

Example
public class Calculator {
public double add(double number1, double number2) {
return number1 + number2;
}
}

JUnit has many features that make it easy to write and run tests
– Separate test class instances and class loaders for each unit test to avoid side effects
– JUnit annotations to provide resource initialization and reclamation methods; @Before, @BeforeClass, @After, @AfterClass
– A variety of assert methods to make it easy to check the results of your tests
– Integration with other popular tools like ANT, MAVEN, and popular IDEs like Eclipse, NetBeans, IntelliJ and JBuilder

import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest { (1)
@Test
public void testAdd() { (2)
Calculator calculator = new Calculator(); (3)
double result = calculator.add(10, 50); (4)
assertEquals(60, result, 0); (5)
}
}

At 1, we start by defining a test class. The only restriction is that the class must be public; we can name it whatever we like.

At 2, we mark the method as a unit test method by adding the @Test annotation.
A best practice is to name test methods following the testXXX pattern. JUnit doesn’t
have method name restrictions. You can name your methods as you like; as long as
they have the @Test annotation, JUnit will execute them.

At 3, we start the test by creating an instance of the Calculator class (the “object
under test”), and at 4, as before, we execute the test by calling the method to test,
passing it two known values.
At 5, the JUnit framework begins to shine! To check the result of the test, we call
an assertEquals method, which we imported with a static import on the first line of
the class.

 

Assert Method

– assertTrue(String message, Boolean test)

– assertFalse(String message, Boolean test)

– assertNull(String message, Object object)

– assertNotNull(String message, Object object)

– assertEquals(String message, Object expected, Object actual) (uses equals method)

– assertSame(String message, Object expected, Object actual) (uses == operator)

– assertNotSame(String message, Object expected, Object actual)

 

 

Leave a Reply