Order of execution in junit

Viewed 35

My code

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

@TestMethodOrder(OrderAnnotation.class)
public class Order {

    @Test
    @Order(4)
    public void test1() {
        System.out.println("This is test 1");
    }

    @Test
    @Order(3)
    public void test2() {
        System.out.println("This is test 2");
    }

    @Test
    @Order(2)
    public void test3() {
        System.out.println("This is test 3");
    }

    @Test
    @Order(1)
    public void test4() {
        System.out.println("This is test 4");
    }
}

The output of the code

This is test 1
This is test 2
This is test 3
This is test 4

Does anyone know what is wrong with the code? I need to execute it in the order that I provided.

1 Answers

Change the first import - import org.junit.Test; to import org.junit.jupiter.api.Test;, that is the issue here. I've got output as:

This is test 4
This is test 3
This is test 2
This is test 1
Related