x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    JUnit 5 Tutorial

    When you write business logic, you may tend to test it using actual test scenario like performing some action from UI or call the API from tool like Postman, but it is always good practice to have a suite of JUnit tests to quickly test your code. Writing JUnit test gives more confidence that code actually works!. Also if you write JUnit for an existing piece of code, it provides you good understanding of code.

    JUnit is a standard framework for developing unit tests in Java.JUnit tests are programs to test the business logic of a program and helps in automating tests for code. Apart from testing existing code, JUnit test suite is also helpful in checking if anything is broken due to any code modifications.

    In this tutorial, we will show how to write a JUnit tests using JUnit 5 framework.

    How to write a basic JUnit 5 test

    Step 1) Add maven JUnit 5 dependencies

    To run a JUnit 5 test, you need below dependencies:

    Maven dependencies

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.8.2</version>
        <scope>test</scope>
    </dependency>    

    Gradle dependencies

    dependencies {
    
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
    
        testImplementation 'org.junit.platform:junit-platform-launcher:1.8.2'
    
    }

    Step 2) Write Calculator class with method multiply

    In the example below, we will first write a Calculator class which has one method multiply, and then we will write a JUnit 5 Test class to test whether multiply method actually works or not.

    public class Calculator {
        
        public double multiply(double number1, double number2) {
            return number1 * number2;
        }
    }    

    Step 3) Write JUnit 5 CalculatorTest class which tests multiply() method of Calculator Class

    Typically a JUnit test class ends with Test as suffix, so our JUnit class name will be CalculatorTest.

    JUnit framework will identify a method as test only if you mark the method as @Test annotation. CalculatorTest class below will create an instance of Calculator class and call its multiply method. To validate if the result is correct we use assertEquals method provided by the Assert class in which we provide the expected result as one of the arguments and actual result as other argument.

    // JUnit test which tests methods of Calculator class , it checks if result expected is correct   
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import org.junit.jupiter.api.Test;
    
    public class CalculatorTest {
    
        @Test
        public void testMultiply() {
            Calculator calculator = new Calculator();
            double result = calculator.multiply(11, 12);
            assertEquals(132, result, 0);
        }
    
    }    


    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This