Junit 5 Test Execution @Order Annotation

@Order annotation make sure that test cases will execute by the order number provided as a parameter to the @Order(1) annotation.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static org.junit.jupiter.api.Assertions.assertEquals;

@TestMethodOrder(OrderAnnotation.class)
class AppleCalculatorTest {

    @Test
    @Order(5)
    void addAppleTestCase1() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @Order(4)
    void addAppleTestCase2() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
    }

    @Test
    @Order(3)
    void addAppleTestCase3() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(6, appleCalculator.addApple(3, 3), "3 apple + 3 apple is 6 apple");
    }

    @Test
    @Order(2)
    void addAppleTestCase4() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(8, appleCalculator.addApple(4, 4), "4 apple + 4 apple is 8 apple");
    }

    @Test
    @Order(1)
    void addAppleTestCase5() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(10, appleCalculator.addApple(5, 5), "5 apple + 5 apple is 10 apple");
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

follow us on