Annotations in JUnit

Annotations in JUnit


Posted in : Java Posted on : November 24, 2010 at 6:43 PM Comments : [ 0 ]

This section contains the detail about Annotations in JUnit.

Annotations in JUnit

Annotations are like meta-tags that you can add to you code and apply them to methods or in class. These annotation in JUnit gives us information about test methods , which methods are going to run before & after test methods, which methods run before & after all the methods, which methods or class will be ignore during execution, which methods are suspected to throw exception etc.

Annotation are added in JUnit 4 version & you can found it in all the releases after this version. Annotations reduces coding & extra burden from tester. The annotations in JUnit are predefined and need not to define, you can implement it directly. Also you can have your own defined annotations in class. But for testing , annotations are predefined in JUnit.

Given below list of annotations and their meaning in JUnit :

Annotation Description

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

@Before

Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method.

@After

If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method.

@BeforeClass

Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class. This can be used to perform computationally expensive setup (like logging into a database).

@AfterClass

Will perform the method after all tests have finished. This can be used to perform clean-up activities for example be used to disconnect to a database

@Ignore

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed.


Example

In the given below example, we will test a method Add() using another test class. You will notice that @ Test annotation is applied to testAdd() method which is used for testing Add() method of Addition class.

Given below the code of both the classes & output of the test :

Addition.java

package devmanuals.junit;

public class Addition {
int c;
int Add(int a, int b) {
c = a + b;
return c;
}
}

AdditionTest.java(Test class to test Addition class)

package devmanuals.junit;

import static org.junit.Assert.*;

import org.junit.Test;

public class AdditionTest {
	Addition s = new Addition();
	@Test
	public void testAdd() {
		int a = 3, b = 6;
		int expectedOutput = (a + b);
		assertEquals(expectedOutput, s.Add(a, b));
	}

}

Output

After running Test Class following output will appear :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics