Writing unit tests
Posted on: February 4, 2011 at 12:00 AM
In this tutorial you will learn how to write a unit test using Junit framework

Writing Unit Tests

Unit testing is a way through which your small block of code. Unit testing is not used to find the bugs. It allows the developers to test their deviation from their goal/ aim or to check whether the method performs the correct computation or it is calculating required result or not.

You will learn that how to write a unit test case using junit frame work.

There are some naming convention which should be followed during writing a test case. When when you are going to write a test case for a class then you should write the class name ending with 'Test' word. for example the test class name for class Example should be ExampleTest. If you write like this then it will not give an error. When you write a test case for the method then the method name should be start with the key word 'test'. for example if you want to write a test case for method 'add' then the name of this method in the Test class should be testAdd. The test word is added before the Method name and the fist letter of the method name becomes capital. If this 'test' precede before the method name, then unit testing will not formed with this method. ADS_TO_REPLACE_1

Before writing a Test Case you must download and include the junit jar file. You can download jar file from Junit site and include this file into your project.

Now write a test class and extend the class TestCase of junit.framework.TestCase. The class TestCase extends the class Assert and implements the interface Test. The Assert class consist a set of assertion method which is used to write a test case, such as assertTrue(........), assertEquals(........) etc.

Then define a instance variable, and store the state of fixture in this variable.ADS_TO_REPLACE_2

Override the method setUp() and tearDown() of class TestCase. Use setUp() method to initialize the fixture state and use tearDown() method to clean up the test.

Now consider a simple java class which has a method addString, this method adds two string.

ExampleClass.javaADS_TO_REPLACE_3

package net.roseindia;

public class ExampleClass {
	static String s;

	public static String adString(String x, String y) {
		s = x.concat(y);
		return s;
	}

}

Now the test class for this is

ExampleClassTest.java

package net.roseindia;

import junit.framework.TestCase;

public class ExampleClassTest extends TestCase {

	String fistString;
	String secondString;
	String finalResult;
	String result;

	@Override
	protected void setUp() throws Exception {
		// Initializing Values
		fistString = "rose";
		secondString = "india";
		finalResult = "roseindia";

		// Calculating Value from method
		result = ExampleClass.adString(fistString, secondString);
		super.setUp();
	}

	public void testSum() {
		// Testing Value
		assertEquals(result, finalResult);
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
	}
}


When you will run this program using eclipse as a Junit Test case, the output will look like this.ADS_TO_REPLACE_4

 
 

You can download the source code and configure it into your eclipse.

Download Select Source Code

Related Tags for Writing unit tests :

Advertisements

Ads

 
Advertisement null

Ads