0

Can anyone tell me what is the order of evaluation of @Test annotation in Junit when you have multiple @Test Annotation? I was trying with the following example but didnot find any specific order.You may consider the following example to explain your answer.

package test_Cases;

import org.junit.Test;

public class First_test_case {
    @Test
public void apsTest(){
    System.out.println("THIS IS FIRST TEST CAES.");
    //selenium code
}
@Test
public void appletestTest(){
    System.out.println("THIS IS second TEST CAES.");
    //selenium code
}
@Test
public void aboutestTest(){
    System.out.println("THIS IS third TEST CAES.");
    //selenium code
}

@Test
public void dtestTest(){
    System.out.println("THIS IS fourth TEST CAES.");
    //selenium code
}@Test
public void etestTest(){
    System.out.println("THIS IS fifth TEST CAES.");
    //selenium code
}@Test
public void ftestTest(){
    System.out.println("THIS IS sixth TEST CAES.");
    //selenium code
}


}
James Dunn
  • 7,548
  • 11
  • 47
  • 81
  • possible duplicate of [How to run test methods in specific order in JUnit4?](http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4) – Joachim Sauer Oct 30 '13 at 07:26
  • That's not implemented in JUnit (because the author thinks you should not depend on the order of your tests). If you absolutely need it, either use a third-party test runner or switch to another framework such as TestNG. (edit: I just saw that Junit 4.11 provides a solution! See the linked-to question for details). – Joachim Sauer Oct 30 '13 at 07:27
  • @JoachimSauer is right, being "independent" is one of the properties of good tests. You can read here http://www.cavdar.net/2008/10/25/properties-of-good-tests-a-trip/ a description of the others as well – Morfic Oct 30 '13 at 07:49

1 Answers1

5

Using @FixMethodOrder JUnit annotation we can achieve ordered execution of the junit tests

Note: We need to use JUnit 4.11 or later versions to get this option

Example Code Snippet:

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

// Test execution order : ascending order of method names
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedJUnitTest {
    @Test
    public void second() {
        System.out.println("Inside second test");
    }

    @Test
    public void first() {
        System.out.println("Inside first test");
    }

    @Test
    public void third() {
        System.out.println("Inside third test");
    }
}

Output:

Inside first test
Inside second test
Inside third test

Similarly we can use @FixMethodOrder(MethodSorters.DEFAULT) and @FixMethodOrder(MethodSorters.JVM) as well which will order the test execution.

Visit https://github.com/junit-team/junit/wiki/Test-execution-order to get more insight on this.

Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139
Sitam Jana
  • 3,015
  • 19
  • 36