Open In App

JUnit 5 – Execute Test in Eclipse

JUnit is one of the widely used unit testing frameworks for Java-based Applications. We have different versions and upgrades published by JUnit 5, the latest version available in JUnit is JUnit 5. JUnit 5 is the upgraded version of JUnit 4, which is launched to support various latest features of Java like lambda expressions, etc. So, if you want to use JUnit 5 in your testing make sure that you are running a modern version of Java where Java 8 is the minimum version it can be.

Prerequisites

Submodules in JUnit 5

To propose modular development and easy to maintain, the JUnit team launched this JUnit 5 as three different submodules, where its predecessor won’t work like that.The three Submodules are mentioned below:



Key Features of JUnit 5

The latest JUnit 5 comes with different modern features. Let’s us see the key features of JUnit 5.

To display our custom string in the test result or report use the DisplayName annotation and pass a string to it.



@DisplayName("Function to test 31 is prime or not")
@Test
void testPrimeNumbers() {
// test logic
assertTrue(31, Utility.isPrime(31));
}
// the above method's test report will contain the string
// in the DisplayName annotation rather the function name.

Creating a Java Project

Creating JUnit 5 Project

In the new Project config dialog box, enter your configuration.

Setting the project config

Setting Up JUnit 5 in Eclipse

Setting up classpath

Selecting junit library

Selecting Junit 5

JUnit5 in the Classpath

Junit 5 project file structure

Creating a simple class to test it using JUnit 5

Creating a utility class to test

Creating the class




// Sample utility package
package com.example.utils;
  
// A simple utility class with two feature methods
public class Utilities {
    // function to check whether a number is prime
    public static boolean isPrime(long n)
    {
        // optimised loop for sqrt(n)
        for (long val = 2; val * val <= n; val++) {
             // checking the divisibility of n with val
            if (n % val == 0) {
                return false;
            }
        }
        return n > 1;
    }
    // function to check the given number is even
    public static boolean isEven(long n)
    {
        return !((n & 1) == 1);
    }
}

Creating test case in JUnit 5

creating junit test case

configuring test class

selecting methods




// sample test package
package com.example.test;
  
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.example.utils.Utilities;
import org.junit.jupiter.api.Test;
  
  
// A JUnit Test class to test
// Utilities Class
class UtilitiesTest {
    
    // test 1 for checking the static isPrime method
    // from Utilities class
    // Test annotations are being used to create JUnit tests
    @Test
    void testIsPrime1(){
        assertTrue(Utilities.isPrime(13));
    }
    
    // test 2 to check the isprime method
    @Test
    void testIsPrime2(){
        assertFalse(Utilities.isPrime(18));
    }
    
    // test 1 to check the static isEven method
    // from Utilities class
    @Test
    void testIsEven1(){
        assertFalse(Utilities.isEven(19));
    }
    
    // test 2 to check the isEven method
    @Test
    void testIsEven2(){
        assertTrue(Utilities.isEven(80));
    }
}

Running JUnit 5 Tests:

Running JUnit 5 test

Output:

Junit test output


Article Tags :