forked from junit-team/junit4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SortMethodsWith allows the user to choose the order of execution of t…
…he methods within a test class. The default order of execution of JUnit tests within a class is deterministic but not predictable. Before 4.11, the behaviour was to run the test methods in byte code order, which pre-Java 7 was mostly predictable. Java 7 (and some previous versions), does not guaranteee the order of execution, which can change from run to run, so a deterministic sort was introduced. As a rule, test method execution should be independent of one another. However, there may be a number of dependent tests either through error or by design. This class allows the user to specify the order of execution of test methods. There are four possibilities: MethodSorters.DEFAULT: the default value, deterministic, but not predictable MethodSorters.JVM: the order in which the tests are returned by the JVM, i.e. there is no sorting done MethodSorters.NAME_ASC: sorted in order of method name, ascending MethodSorters.NAME_DESC: sorter in order of method name, descending
- Loading branch information
1 parent
db9a456
commit c610a49
Showing
4 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
package org.junit; | |||
|
|||
import java.lang.annotation.ElementType; | |||
import java.lang.annotation.Retention; | |||
import java.lang.annotation.RetentionPolicy; | |||
import java.lang.annotation.Target; | |||
|
|||
import org.junit.runners.MethodSorters; | |||
|
|||
/** | |||
* SortMethodsWith allows the user to choose the order of execution of the methods within a test class. | |||
* <br/> | |||
* <br/> | |||
* The default order of execution of JUnit tests within a class is deterministic but not predictable. | |||
* Before 4.11, the behaviour was to run the test methods in byte code order, which pre-Java 7 was mostly predictable. | |||
* Java 7 (and some previous versions), does not guaranteee the order of execution, which can change from run to run, | |||
* so a deterministic sort was introduced. | |||
* <br/> | |||
* As a rule, test method execution should be independent of one another. However, there may be a number of dependent tests | |||
* either through error or by design. This class allows the user to specify the order of execution of test methods. | |||
* <br/> | |||
* There are four possibilities: | |||
* <ul> | |||
* <li>MethodSorters.DEFAULT: the default value, deterministic, but not predictable</li> | |||
* <li>MethodSorters.JVM: the order in which the tests are returned by the JVM, i.e. there is no sorting done</li> | |||
* <li>MethodSorters.NAME_ASC: sorted in order of method name, ascending</li> | |||
* <li>MethodSorters.NAME_DESC: sorter in order of method name, descending</li> | |||
* </ul> | |||
* | |||
* Here is an example: | |||
* | |||
* <pre> | |||
* @SortMethodsWith(MethodSorters.NAME_ASC) | |||
* public class MyTest { | |||
* } | |||
* </pre> | |||
* | |||
* @see org.junit.runners.MethodSorters | |||
*/ | |||
@Retention(RetentionPolicy.RUNTIME) | |||
@Target({ElementType.TYPE}) | |||
public @interface SortMethodsWith { | |||
/** | |||
* Optionally specify <code>sorter</code> to have the methods executed in a particular order | |||
*/ | |||
MethodSorters value() default MethodSorters.DEFAULT; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
package org.junit.runners; | |||
|
|||
import java.lang.reflect.Method; | |||
import java.util.Comparator; | |||
|
|||
import org.junit.internal.MethodSorter; | |||
|
|||
/** | |||
* Sort the methods into a specified execution order | |||
*/ | |||
public enum MethodSorters { | |||
/** Name ascending */ | |||
NAME_ASC(MethodSorter.NAME_ASC), | |||
/** Name descending */ | |||
NAME_DESC(MethodSorter.NAME_DESC), | |||
/** default JVM, (no sort) */ | |||
JVM(null), | |||
/** Default, deterministic but not predictable */ | |||
DEFAULT(MethodSorter.DEFAULT); | |||
|
|||
private final Comparator<Method> fComparator; | |||
|
|||
private MethodSorters(Comparator<Method> comparator) { | |||
this.fComparator= comparator; | |||
} | |||
|
|||
public Comparator<Method> getComparator() { | |||
return fComparator; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,34 +1,99 @@ | |||
package org.junit.internal; | package org.junit.internal; | ||
|
|
||
import java.lang.reflect.Method; | |||
import java.util.Arrays; | import java.util.Arrays; | ||
|
|||
import org.junit.SortMethodsWith; | |||
import org.junit.Test; | import org.junit.Test; | ||
import org.junit.runners.MethodSorters; | |||
import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||
|
|
||
public class MethodSorterTest { | public class MethodSorterTest { | ||
private static class Dummy { | |||
Object alpha(int i, double d, Thread t) {return null;} | |||
void beta(int[][] x) {} | |||
int gamma() {return 0;} | |||
void gamma(boolean b) {} | |||
void delta() {} | |||
void epsilon() {} | |||
} | |||
private static class Super { | |||
void testOne() {} | |||
} | |||
private static class Sub extends Super { | |||
void testTwo() {} | |||
} | |||
|
|||
private String toString(Class<?> clazz, Method[] methods) { | |||
return Arrays.toString(methods).replace(clazz.getName() + '.', ""); | |||
} | |||
|
|||
private String declaredMethods(Class<?> clazz) { | |||
return toString(clazz, MethodSorter.getDeclaredMethods(clazz)); | |||
} | |||
|
|
||
@Test public void getDeclaredMethods() throws Exception { | @Test public void getMethodsNullSorter() throws Exception { | ||
assertEquals("[void epsilon(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread), void delta(), int gamma(), void gamma(boolean)]", declaredMethods(Dummy.class)); | assertEquals("[void epsilon(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread), void delta(), int gamma(), void gamma(boolean)]", declaredMethods(Dummy.class)); | ||
assertEquals("[void testOne()]", declaredMethods(Super.class)); | assertEquals("[void testOne()]", declaredMethods(Super.class)); | ||
assertEquals("[void testTwo()]", declaredMethods(Sub.class)); | assertEquals("[void testTwo()]", declaredMethods(Sub.class)); | ||
} | } | ||
|
|
||
private static String declaredMethods(Class<?> c) { | @SortMethodsWith(MethodSorters.DEFAULT) | ||
return Arrays.toString(MethodSorter.getDeclaredMethods(c)).replace(c.getName() + '.', ""); | private static class DummySortWithDefault { | ||
Object alpha(int i, double d, Thread t) {return null;} | |||
void beta(int[][] x) {} | |||
int gamma() {return 0;} | |||
void gamma(boolean b) {} | |||
void delta() {} | |||
void epsilon() {} | |||
} | } | ||
|
|
||
private static class Dummy { | @Test public void testDefaultSorter() { | ||
assertEquals("[void epsilon(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread), void delta(), int gamma(), void gamma(boolean)]", declaredMethods(DummySortWithDefault.class)); | |||
} | |||
|
|||
@SortMethodsWith(MethodSorters.JVM) | |||
private static class DummySortJvm { | |||
Object alpha(int i, double d, Thread t) {return null;} | Object alpha(int i, double d, Thread t) {return null;} | ||
void beta(int[][] x) {} | void beta(int[][] x) {} | ||
int gamma() {return 0;} | int gamma() {return 0;} | ||
void gamma(boolean b) {} | void gamma(boolean b) {} | ||
void delta() {} | void delta() {} | ||
void epsilon() {} | void epsilon() {} | ||
} | } | ||
private static class Super { |
|
||
void testOne() {} | @Test public void testSortWithJvm() { | ||
Class<?> clazz = DummySortJvm.class; | |||
String actual = toString(clazz, clazz.getDeclaredMethods()); | |||
|
|||
assertEquals(actual, declaredMethods(clazz)); | |||
} | |||
|
|||
@SortMethodsWith(MethodSorters.NAME_ASC) | |||
private static class DummySortWithNameAsc { | |||
Object alpha(int i, double d, Thread t) {return null;} | |||
void beta(int[][] x) {} | |||
int gamma() {return 0;} | |||
void gamma(boolean b) {} | |||
void delta() {} | |||
void epsilon() {} | |||
} | } | ||
private static class Sub extends Super { |
|
||
void testTwo() {} | @Test public void testNameAsc() { | ||
assertEquals("[java.lang.Object alpha(int,double,java.lang.Thread), void beta(int[][]), void delta(), void epsilon(), int gamma(), void gamma(boolean)]", declaredMethods(DummySortWithNameAsc.class)); | |||
} | |||
|
|||
@SortMethodsWith(MethodSorters.NAME_DESC) | |||
private static class DummySortWithNameDesc { | |||
Object alpha(int i, double d, Thread t) {return null;} | |||
void beta(int[][] x) {} | |||
int gamma() {return 0;} | |||
void gamma(boolean b) {} | |||
void delta() {} | |||
void epsilon() {} | |||
} | } | ||
|
|
||
@Test public void testNameDesc() { | |||
assertEquals("[int gamma(), void gamma(boolean), void epsilon(), void delta(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread)]", declaredMethods(DummySortWithNameDesc.class)); | |||
} | |||
} | } |