JUnit Annotations with Example

JUnit is a unit testing framework for the Java programming language. JUnit is used primarily to test each and every unit or component of your application, like classes and methods. It helps to write and run repeatable automated tests to ensure your code runs as intended.

Introduction

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.

JUnit5 is the next generation of JUnit. The goal is to create an up-to-date foundation for developer-side testing on the JVM. This includes focusing on Java 8 and above, as well as enabling many different styles of testing.With the knowledge of annotations in JUnit5, one can easily learn and implement a JUnit test.

JUnit offers integration with CI/CD tools such as Jenkins, Teamcity etc. to help you create a sturdy delivery pipeline.

Basic Java Annotations

So here are some basic JUnit annotation that are mostly used and required for unit testing :

  • @Test – This is the test method to run, public void
  • @BeforeClass – This annotation is used if you want to execute some statements before all the test cases for e.g. test connection must be executed before all the test cases., public static void
  • @AfterClass – This annotation can be used if you want to execute some statements after all test cases for e.g. Releasing resources after executing all test case, public static void
  • @Before – This annotation is used if you want to execute some statement such as preconditions before each test case, public void
  • @After – This annotation can be used if you want to execute some statements after each Test Case for e.g resetting variables, deleting temporary files ,variables, etc, public void

A simple example will help us to understand the working of JUnit annotations.

Java Class to be tested

We are going to use a simple example regarding Account balance where we want to perform our unit testing.

package com.thecodersstop.junit;
 
public class Account {
    private double bal;
 
    public Account(double bal) {
        this.bal = bal;
    }
 
    public double getBal() {
        return bal;
    }
 
    public void setBal(double bal) {
        this.bal = bal;
    }
 
    public void withdraw(double withdrawAmount) {
        this.bal = bal - withdrawAmount;
    }
 
    public void deposit(double depositAmount) {
        this.bal = bal + depositAmount;
    }
}

Junit Test Cases

Here we have our test cases for the Account java class. This test class includes all the basic annotations mentioned above.

package com.thecodersstop.junit;

import org.junit.*;
 
public class AccountTest extends Assert {
    private Account account;
    private static double bal;
 
    @BeforeClass
    public static void BeforeClass() {
		// Code executed before the first test method
        bal = 100;
        System.out.println("Before Class");
    }
 
    @Before
    public void beforeTest() throws Exception {
		// Code executed before each test
        account = new Account(bal);
    }
 
    @Test
    public void testCase1() {
        Assert.assertEquals("Test balance", account.getBal(), bal, 0);
        System.out.println("Test balance. Balance: " + account.getBal());
    }
 
    @Test
    public void testCase2() {
        account.deposit(10);
        Assert.assertEquals("Test deposit", account.getBal(), bal, 10);
        System.out.println("Test deposit. Balance: " + account.getBal());
    }
 
    @Test
    public void testCase3() {
        account.withdraw(30);
        Assert.assertEquals("Test withdraw", account.getBal(), bal, 30);
        System.out.println("Test withdraw. Balance: " + account.getBal());
    }
 
    @After
    public void afterTest() throws Exception {
		// Code executed after each test
        account = null;
        System.out.println("refresh");
    }
 
    @AfterClass
    public static void AfterClass() {
		// Code executed after the last test method
        balance = 0;
        System.out.println("After Class");
    }
}

Run the JUnit Test Cases

Here is the output of our test cases. As we can see, the sequence of the executed test methods, complies with what we described in the starting. This JUnit test is fully passed.

Before Class
Test balance. Balance: 100.0
refresh
Test deposit. Balance: 110.0
refresh
Test withdraw. Balance: 70.0
refresh
After Class

JUnit offers integrations with IDEs such as Eclipse, IntelliJ etc. so you could test run your code quickly and easily.

Execution Sequence of JUnit Annotations

Here is a basic process flowchart of JUnit annotations that will help you to understand the flow, step by step.

@Ignores – This annotation can be used if you want to ignore some statements during test execution for e.g. disabling some test cases during test execution.

I hope you have enjoyed this post and it helped you to understand the basics of JUnit annotations. Please like and share and feel free to comment if you have any suggestions or feedback.

One comment

  1. […] with the JUnit framework. In case your are not familiar with JUnit please check the following JUnit article.Mockito doesn’t give you hangover because the tests are very readable and they produce clean […]

Leave a Reply

%d bloggers like this: