JUnit 5 – Basics to start with…

Unit Testing of the code is mandatory in every project. Each line of code you write, should be tested with JUnit Test Cases. Many projects have been following Test Driven Development(TDD) model successfully for a very long time. JUnit 5 comes with many new features to make developer’s job easy.

JUnit 5 is basically amalgamation of three key modules namely –

  • JUnit Platform
  • JUnit Jupiter
  • JUnit Vintage

Following are the dependencies required for JUnit 5 –

    <properties>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <junit.platform.version>1.5.2</junit.platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Enter fullscreen mode Exit fullscreen mode

Let’s now write a small Java class which we can utilise to learn basics of JUnit 5 –

public class Junit5Basics {
    public static int calculate(int number1, 
                                int number2, 
                                String operation) {

        if("add".equalsIgnoreCase(operation))
            return number1 + number2;

        if("subtract".equalsIgnoreCase(operation))
            return number1 - number2;

        return 0;
    }
}

Enter fullscreen mode Exit fullscreen mode

Please find below the sample JUnit 5 test cases class for the above class which is pretty self explanatory. I have added meaningful statements so that you can easily understand. You can see new annotations being used which were not there in earlier JUnit versions –

import org.junit.jupiter.api.*;

public class Junit5BasicsTest {

    @BeforeAll
    static void setUp() {
        System.out.println("This method is going to run before everything once");
    }

    @BeforeEach
    void setupBeforeEachTest(){
        System.out.println("This method is going to run before each test");
    }

    @Test
    void testAddition() {
        System.out.println("Testing the Addition functionality");
        Assertions.assertEquals( 8 , Junit5Basics.calculate(4, 4, "add"));
    }

    @Test
    void testSubtraction() {
        System.out.println("Testing the Subtraction functionality");
        Assertions.assertEquals( 4 , Junit5Basics.calculate(8, 4, "subtract"));
    }

    @AfterEach
    void tearAfterEachTest(){
        System.out.println("This method is going to run after each test");
    }

    @AfterAll
    static void tear(){
        System.out.println("This method is going to run after everything once");
    }
}

Enter fullscreen mode Exit fullscreen mode

Please find below the sample output to verify your understanding –

This method is going to run before everything once

This method is going to run before each test
Testing the Addition functionality
This method is going to run after each test


This method is going to run before each test
Testing the Subtraction functionality
This method is going to run after each test

This method is going to run after everything once

Enter fullscreen mode Exit fullscreen mode

I think you have understood the basics of JUnit 5. In the future posts, I’ll try to cover other advanced topics related to JUnit 5.

原文链接:JUnit 5 – Basics to start with…

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容