JUnit Annotations

It is a special form of syntactic meta-data that can be added to Java source code for better code readability and structure. Variables, parameters, packages, methods, and classes can be annotated.

Annotations were introduced in Junit4, which makes Java code more readable and simple. This is the big difference between Junit3 and Junit4 that Junit4 is annotation based.

With the knowledge of annotations in Junit5, one can easily learn and implement a JUnit testing


Annotations for Junit testing

The Junit framework is annotation based, so let’s see the annotations that can be used while writing the test cases.

@Test annotation specifies that the method is the test method.

@Test(timeout=1000) annotation specifies that the method will be failed if it takes longer than 1000 milliseconds (1 second).

@BeforeClass annotation specifies that the method will be invoked only once, before starting all the tests.

@Before annotation specifies that the method will be invoked before each test.

@After annotation specifies that the method will be invoked after each test.

@AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.


Example Of Anotation Based Example:

package guru99.junit;        

import static org.junit.Assert.assertEquals;               

import static org.junit.Assert.assertFalse;           

 import java.util.ArrayList;       

 import org.junit.After;       

import org.junit.AfterClass;       

import org.junit.Before;       

import org.junit.BeforeClass;       

import org.junit.Ignore;       

import org.junit.Test;       


public class JunitAnnotationsExample {               

    private ArrayList<String> list;                   


    public static void m1() {                           

        System.out.println(“Using @BeforeClass , executed before all test cases “);                   



    public void m2() {                   

        list = new ArrayList<String>();                   

        System.out.println(“Using @Before annotations ,executed before each test cases “);                   



    public static void m3() {                           

        System.out.println(“Using @AfterClass ,executed after all test cases”);                   




    public void m4() {                   


        System.out.println(“Using @After ,executed after each test cases”);                   



    public void m5() {                   



        assertEquals(1, list.size());           



    public void m6() {                   

        System.out.println(“Using @Ignore , this execution is ignored”);                   



    @Test(timeout = 10)           

    public void m7() {                   

        System.out.println(“Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case”);                   



    @Test(expected = NoSuchMethodException.class)                   

    public void m8() {                   

        System.out.println(“Using @Test(expected) ,it will check for specified exception during its execution”);