Why Junit Cases not run in order in JDK 7.

#java

Conclusion first:

Junit uses reflection for getting and executing test methods. Junit get list of all tests using “Method[] getDeclaredMethods()” from java.lang.Class.

The elements in the array returned by getDeclaredMethods are not sorted and are not in any particular order.

How to make executions order fixed

Since JUnit 4.11, you can do:

 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class MyTest {
 }

Reference: MethodSorters
Annotation Type FixMethodOrder