JUnit Test Order---Fixmethodorder

Source: Internet
Author: User
Tags sorts

Reference: http://www.cnblogs.com/lukehuang/archive/2013/08/27.html

Brief
Junit 4.11 Adds an attribute that specifies the order in which test methods are executed

The order of execution of the test class can be specified by adding the annotation "@FixMethodOrder (value)" to the test class, where value is the order of execution

Three execution sequences to choose from: Default (Methodsorters.default), by method name (methodsorters.name_ascending) and JVM (METHODSORTERS.JVM)

When no order is specified, it is performed by default
Sorters
1. Methodsorters.default
The default order is determined by the method name Hashcode value, and if the hash value is the same size, the dictionary order of the first name determines
Because Hashcode is generated and operating system-dependent (native-modified), there may be a different order of execution for various operating systems, with the same order of execution on one operating system
Implementation code:

Copy Code
/**
* DEFAULT sort order
*/
public static comparator<method> DEFAULT = new Comparator<method> () {
public int Compare (method m1, method m2) {
int i1 = M1.getname (). Hashcode ();
int i2 = M2.getname (). Hashcode ();
if (i1! = i2) {
return I1 < I2? -1:1;
}
Return Name_ascending.compare (M1, M2);
}
};
Copy Code
2. Methodsorters.name_ascending (recommended)
The order of execution is always consistent in this way because it is sorted by the name of the method, because it is in the dictionary order of the characters;
However, this approach requires a certain naming convention for test methods, such as test methods that begin with TESTNNN (NNN indicates the test method serial number 001-999)

Copy Code
/**
* Method name Ascending lexicographic sort order, with {@link method#tostring ()} as a tiebreaker
*/
public static comparator<method> name_ascending = new Comparator<method> () {
public int Compare (method m1, method m2) {
Final int comparison = M1.getname (). CompareTo (M2.getname ());
if (comparison! = 0) {
return comparison;
}
Return m1.tostring (). CompareTo (M2.tostring ());
}
};
Copy Code
3. METHODSORTERS.JVM
Executes in the order of the name of the method returned by the JVM, in which case the order of execution of the test methods is unpredictable, i.e. the order of each run may be different (especially in JDK7).
Samples
The following are the results of the execution of win7-jdk7-junit4.11

Copy Code
@FixMethodOrder (Methodsorters.default)
@FixMethodOrder (methodsorters.name_ascending)
@FixMethodOrder (METHODSORTERS.JVM)
public class Testjunitorder {

@Test
public void Test003third () {

System.out.println ("Test003third");
}

@Test
public void Test001first () {

System.out.println ("Test001first");
}

@Test
public void Test002second () {

System.out.println ("Test002second");
}
}
Copy Code
1. DEFAULT
The result is always:
Test002second
Test001first
Test003third

2. name_ascending
The result is always:
Test001first
Test002second
Test003third

3. JVM
In most cases the result is:
Test002second
Test001first
Test003third
Occasionally appear:
Test001first
Test003third
Test002second
Dig more.
In fact, JUnit uses a reflection mechanism to get all the test methods in a junit, and generates an array of methods, followed by the test methods in the array;
And when the order of execution is specified with annotation, JUnit sorts the methods in the array according to the specified order after it obtains the arrays of the test methods;

Copy Code
public static method[] Getdeclaredmethods (class<?> clazz) {
comparator<method> Comparator = Getsorter (clazz.getannotation (Fixmethodorder.class));//Gets the execution order specified by the test class

Method[] methods = Clazz.getdeclaredmethods ();
if (comparator! = null) {
Arrays.sort (methods, comparator);//Sort by specified order
}

return methods;
}
Copy Code
The three execution sequences are defined as follows:

Copy Code
/**
* Sorts the test methods by the method name, in lexicographic order,
* With {@link method#tostring ()} used as a tiebreaker
*/
Name_ascending (methodsorter.name_ascending),

/**
* Leaves the test methods in the order returned by the JVM.
* Note that the order from the JVM could vary from run to run
*/
JVM (NULL),

/**
* Sorts the test methods in a deterministic, and not predictable, order
*/
DEFAULT (Methodsorter.default);
Copy Code
As can be seen when set to METHODSORTERS.JVM, it does not provide a comparator implementation, so the order of execution method is actually clazz.getdeclaredmethods (); The order of the methods in the resulting array, Because Java does not specify any order for the methods returned by Getdeclaredmethods, the order in which the JUnit test methods are executed is not deterministic.


---------------------------------------------------------------------
Example:

Java code
  1. Import Org.junit.FixMethodOrder;
  2. Import Org.junit.Test;
  3. Import Org.junit.runners.MethodSorters;
  4. @FixMethodOrder (methodsorters.name_ascending)
  5. Public class Orderedtestcasesexecution {
  6. @Test
  7. public void Test001first () {
  8. SYSTEM.OUT.PRINTLN ("executing first Test");
  9. }
  10. @Test
  11. public void Test002second () {
  12. SYSTEM.OUT.PRINTLN ("executing second Test");
  13. }
  14. @Test
  15. public void Test003third () {
  16. SYSTEM.OUT.PRINTLN ("executing third Test");
  17. }
  18. }



Output:
Executing first Test
Executing second Test
Executing third Test

JUnit Test Order---Fixmethodorder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.