Oct 18, 2014

Annotations in Junit 4 and assert statements

In previous post we saw how to create sample Maven Junit project and used various annotations like @Test, @BeforeClass, @AfterClass, etc. Here we will list down all most commonly used annotations and its use.
Annotation Description and Uses
@Test
public void getEmpSalaryReport(){ ..}

Method annotated with @Test annotation considered as a test method.

@Before
public void setupTestData(){.....}
It is executed before every test method executed in that class. It is used to prepare test environment.(Initialize test data)

@After
public void cleanTestData(){....}
This method is executed after each test methods execution .It is used to clean-up the test environment.(Delete test data and reset value to default)

@BeforeClass
public static void methodName(){....}
It is static method and it is executed only once before the very first test method is executed. It is used to perform some task which is one time activity like database connection and allocation of resources.
@AfterClass
public static void method()

This method is executed after all test methods have been executed. It is used to perform clean-up and resources deallocation. Methods annotated with @BeforeClass and @AfterClass must be static

@Test (expected = Exception.class)
public void testExceptionIsThrown() {
    //exception thrown means, success
  }
Test method execution fails if the method does not throw the named exception.
@Test(timeout=<T>)
public void testUpperLimit(){....}
Test method execution fails if it takes more than T milliseconds.

@Ignore or @Ignore("Message of disabling")
@Ignore
public void exceedingUpperLimit(){..}
Test method annotated with @Ignore is not executed.It is used when some test method is taking more time to execute so as build process, so we can skip execution of test method and revisit it later.



Along with annotations, we have some static methods that need special mention when we are dealing with test methods.Assert class contains assert static methods which are mainly used to match expected output from value returned from method executed inside test method. If values matches Junit declares test passed and  switch to next test method , else throw AssertionException or AssertionError. Below is list of assert methods commonly used :
Assert method/statements Description
assertTrue([message,] condition/outcome from some method)
//message is optional , may be omitted

Check that conditional statement is true or not. If true gracefully executed else java.lang.AssertionError thrown.

assertFalse([message,] boolean condition)
Check that conditional statement is false or not. If fasle gracefully executed else java.lang.AssertionError thrown.



assertNull([message,] object/method call returning object)


Check whether object referenced or returned by method call is null or not. If not null, error thrown.

assertNotNull([message,] object/method call returning object)
Check whether object referenced or returned by method call is null or not. If null, error thrown.


assertEquals([message,] expected, actual(value returned from method call))

Compares expected with actual, if equal gracefully executed else error is thrown.


Notes:  Junit 4 support static import of assert methods (assertEquals , asserNotNull).Static import is very handy when it come to using assert methods many places. Because of static import, assert methods are used without specifying class name Assert.Below sample code demonstrate how to use static import:
// without static imports we have to use Assert in each method call.
Assert.assertEquals("Employee's salary is > 1000", 800, emp.getSalary(emp_id));

//with static import
import static org.junit.Assert.assertEquals
.......
assertEquals("Employee's salary is > 1000", 800, emp.getSalary(emp_id));
Read also :  How to create maven junit project
Location: Hyderabad, Telangana, India