running order of test junit code example
Example 1: ordering test in junit
By adding @TestMethodOrder annotation
on top of class and
than @Order tag on each test
with giving them number
// these are all available option for ordering your tests
//@TestMethodOrder(OrderAnnotation.class)
//@TestMethodOrder(Random.class)
//@TestMethodOrder(MethodName.class) // default options
For Example :
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestOrderingInJunit5 {
@Order(3)
@DisplayName("3. Test A method")
@Test
public void testA(){
System.out.println("running test A");
}
@Order(1)
@DisplayName("1. Test C method")
@Test
public void testC(){
System.out.println("running test C");
}
@Order(4)
@DisplayName("4. Test D method")
@Test
public void testD(){
System.out.println("running test D");
}
@Order(2)
@DisplayName("2. Test B method")
@Test
public void testB(){
System.out.println("running test B");
}
In this case it will print
1. Test C
2. Test B
3. Test A
4. Test D
Example 2: junit test ordering
By adding @TestMethodOrder annotation
on top of class and
than @Order tag on each test
with giving them number
// these are all available option for ordering your tests
//@TestMethodOrder(OrderAnnotation.class)
//@TestMethodOrder(Random.class) //randomly runs
//@TestMethodOrder(MethodName.class) // default options
For Example :
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Order(3)
@DisplayName("3. Test A method")
@Test
public void testA(){
System.out.println("running test A");
}
@Order(1)
@DisplayName("1. Test C method")
@Test
public void testC(){
System.out.println("running test C");
}
@Order(4)
@DisplayName("4. Test D method")
@Test
public void testD(){
System.out.println("running test D");
}
@Order(2)
@DisplayName("2. Test B method")
@Test
public void testB(){
System.out.println("running test B");
}
In this case it will print
1. Test C
2. Test B
3. Test A
4. Test D