top of page

Junit - Annotations Basics

Writer's picture: Bijen AdhikariBijen Adhikari

Annotations in JUnit are used to specify how a test method should be executed as well as to offer additional information about the test case. That means, When we write junit test cases, we write a lot of methods, and each method has one annotation. Based on those annotations, the method is executed at runtime, and the order is determined by the annotation.


The following are some of the most common JUnit annotations:



  1. @BeforeClass:- The first annotation is @BeforeClass. So, Now, this annotation is used to initialize any object that you are going to use in your test case or your running testes. @BeforeClass annotation specifies that method will be executed only once, before starting all the tests.

  2. @Before annotation specifies that method will be executed before each test.

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

  4. @After annotation will be executed every time after each test case. So, whatever you have initialized in @Before that should be released in @After.

  5. @AfterClass annotation will be executed only once but after every test has been executed. Whatever you have initialized in @BeforeClass those object you should release in @AfterClass.

  6. @Ignore: : This annotation is used to mark a test method that you want to skip during test execution.


 

Example:-


package com.bijen.JunitTestCases;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class JunitTestCasesExample1 {

    static int beforeClassCount = 1;
    static int afterClassCount = 1;
    static int beforeCount = 1;
    static int afterCount = 1;

    @BeforeClass
    public static void setUpClass() {
        System.out.println("Execution count of beforeClass method is: " + beforeClassCount++);
    }

    @Before
    public void setUp() {
        System.out.println("Execution count of before method is: " + beforeCount++);
    }

    @Test
    public void test1() {
        System.out.println("\t Code for test1 method goes here");
    }

    @Test
    public void test2() {
        System.out.println("\t Code for test2 method goes here");
    }

    @Test
    public void test3() {
        System.out.println("\t Code for test3 method goes here");
    }

    @Test
    public void test4() {
        System.out.println("\t Code for test4 method goes here");
    }
    
    @Test
    public void test5() {
        System.out.println("\t Code for test5 method goes here");
    }

    @After
    public void tearDown() {
        System.out.println("Execution count of After method is " + afterCount++);
    }

    @AfterClass
    public static void tearDownClass() {
        System.out.println("Execution count of AfterClass method is " + afterClassCount++);
    }
}


 

OUTPUT:

Execution count of beforeClass method is: 1



Execution count of before method is: 1
  Code for test1 method goes here
Execution count of After method is 1



Execution count of before method is: 2
  Code for test2 method goes here
Execution count of After method is 2



Execution count of before method is: 3
  Code for test3 method goes here
Execution count of After method is 3



Execution count of before method is: 4
  Code for test4 method goes here
Execution count of After method is 4



Execution count of before method is: 5
  Code for test5 method goes here
Execution count of After method is 5




Execution count of AfterClass method is 1

 

Here, the BeforeClass method is executed only once and the AfterClass method is also executed only once.

But Before and After method will be executed every time of the test case execution.



 
 
 

Recent Posts

See All

Comments


bottom of page