Open In App

JUnit 5 – @BeforeEach

JUnit 5 is a widely used testing framework in the Java ecosystem. It is the successor of JUnit 4 and is designed to address its limitations. JUnit framework allows the developers to write and run the tests for their Java code. These tests help ensure that the code functions correctly and continues to work as expected as changes are made.

JUnit 5 provides a variety of annotations and one such annotation is @BeforeEach. In this article, let us understand about @BeforeEach annotation in JUnit 5.



@BeforeEach in JUnit 5

@BeforeEach annotation in JUnit 5 is used to mark a method that should execute before each test method in the JUnit test case. It is used to provide initialization for set-up tasks.

Example for @BeforeEach

Below is the implementation of @BeforeEach :






// Java program to explain BeforeEach
  
import org.junit.jupiter.api.BeforeEach;
  
// Testing class
public class Testcase1 {
  
    @BeforeEach void test()
    {
        // code that executes before each test method
    }
  
    // test methods
    @Test public void testMethod2()
    {
        // testing logic
    }
}

Prerequisites:

To understand this, we need to have some prerequisites. They are:

IDE Setup

Here, we are using Eclipse IDE for Java and Web Developers 2023-06. You may also use other platforms like IntelliJ, Spring suite tool, Spring Initializer, etc.

Step By Step Implementation

Step 1: Creation of Project

Recommended Configurations:

For testing for JUnit testing are as follows:

  1. Type: Maven
  2. Packaging: Jar
  3. Java Version: 8
  4. Language: Java

Step 2: Adding dependencies

Let us configure our pom.xml file with the following dependencies:

JUnit Jupiter API:

The JUnit Jupiter API is the part of JUnit 5 framework which provides annotations, assertions, and other features for defining and running test cases and serves as a programming model for writing tests in Java. To add the JUnit Jupiter API in the pom.xml file, copy and paste the following code.




<dependency>
     <groupId>org.junit.jupiter</groupId>
     <artifactId>junit-jupiter-api</artifactId>
     <version>5.8.0</version>
     <scope>test</scope>
</dependency>

Project Setup

Now, that the project setup is ready, let us look into an example to understand how to write and run test cases of JUnit using the Maven tool.




// Java Program for addition
  
package project;
  
// Driver Class
public class Addition {
  
    // Method to output sum of two numbers
    public int sum(int a, int b) { return a + b; }
}

Example:

Test Script: Let us write the first test case i.e. Testcase1, to test our Addition class using JUnit features.




// Java Program for testing
  
package project;
  
import static org.junit.jupiter.api.Assertions.*;
  
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
  
// Testing class
class Testcase1 {
  
    public Addition addition;
  
    @BeforeEach void setUp()
    {
        // Instance of Addition class
        addition = new Addition();
    }
  
    @Test void test1()
    {
        int actual = addition.sum(2, 3);
        int expected = 5;
  
        // Check if the output is correct
        assertEquals(actual, expected);
  
        // print the completion of tescase
        System.out.print("Test is executed");
    }
  
    @Test void test2()
    {
        int actual = addition.sum(2, 3);
        int expected = 6;
  
        // Check if the output is correct
        assertEquals(actual, expected);
  
        // print the completion of tescase
        System.out.print("Test is executed");
    }
}

Explanation:

Project Structure (Recommended)

Ensure that you have followed this project structure to reduce the inconsistencies:

Steps to Run the Application:

To run the Spring Boot application, navigate to your project explorer, right-click on your project > run as > JUnit test case.

Output:


Article Tags :