At first, JUnit did not specify the order in which the test methods were called. method is called in the order returned by the mapped API. However, it is unwise to use the JVM order because the Java platform does not prescribe any particular order, in fact JDK7 returns more or less in random order. Most of the good test code written does not assume a sequence, and a predictable failure on a particular platform is better than a random failure.
Starting with version 4.11, if you want to change the test execution order, simply add a @FixMethodOder comment.
At present, there are three kinds of more common:
@FixMethodOrder (Methodsorters.default): The default order. The hash code value of the method name determines the order of execution. Since the generation of hash codes is related to the OS, there may be a different order of execution for unused OS. On an operating system, the order of execution is constant.
@FixMethodOrder (METHODSORTERS.JVM): The order of execution is determined by the JVM. Of course the execution sequence may not be necessary with each test.
@FixMethodOrder (methodsorters.name_ascending): Determines the order of execution by the dictionary order of the method names.
[Java]View Plaincopy
- Import Org.junit.FixMethodOrder;
- Import Org.junit.Test;
- Import Org.junit.runners.MethodSorters;
- @FixMethodOrder (Methodsorters.default)
- @FixMethodOrder (methodsorters.name_ascending)
- @FixMethodOrder (METHODSORTERS.JVM)
- Public class Testexecuteorder {
- @Test
- public void Test03third () {
- System.out.println ("test03");
- }
- @Test
- public void Test01first () {
- System.out.println ("test01");
- }
- @Test
- public void Test02second () {
- System.out.println ("test02");
- }
- }
Execution results are as follows
1. Nothing is added:
test02
test01
test03
2. @FixMethodOrder (Methodsorters.default):
test02
test01
test03
3. @FixMethodOrder (methodsorters.name_ascending):
test01
test02
test03
4. @FixMethodOrder (METHODSORTERS.JVM):
test03
test01
test02
Or
test02
test01
test03
Test Execution Order