Original link: Test execution order
Original date: December 06, 2012
Translation Date: July 2, 2014
Translation staff: Anchor
Description: Junit4.11 version and later support, recommended upgrade to the latest version.
By design principles, JUnit does not specify the order in which test method calls are performed.
So far, these methods are simply executed in the order returned by the reflection (reflection) API.
However, it is unwise to use the sort provided by the JVM by default, because the Java platform does not specify any regular order,
In fact, JDK 7 may return a random order.
Of course, there is no need to assume any sequence of execution between well-written test code, but sometimes it is better to have a predictable failure on some platforms than a random failure.
Starting with version 4.11, JUnit will default to a determined, but unpredictable, order (Methodsorters.default).
To change the order of the test execution, you only need to use the @FixMethodOrder annotation on the test class (Class) and specify an available Methodsorter:
@FixMethodOrder (METHODSORTERS.JVM): The order in which the execution of test methods is kept is returned by the JVM. The sequence of execution of each test may be different.
@FixMethodOrder (methodsorters.name_ascending) : Sorting according to the method name of the test method, according to the Dictionary sorting rule (ASC, from small to large, increment).