Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #386 from matthewfarwell/sortwith
SortMethodsWith allows the user to choose the order of execution of the methods within a test class
- Loading branch information
Showing
5 changed files
with
186 additions
and
21 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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; | ||
|
||
/** | ||
* This class 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. | ||
* The order of execution is not guaranteed for Java 7 (and some previous versions), and can even change | ||
* from run to run, so the order of execution was changed to be deterministic (in JUnit 4.11) | ||
* <br/> | ||
* It is recommended that test methods be written so that they are independent of the order that they are executed. | ||
* 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/> | ||
* For possibilities, see {@link MethodSorters} | ||
* | ||
* Here is an example: | ||
* | ||
* <pre> | ||
* @FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
* public class MyTest { | ||
* } | ||
* </pre> | ||
* | ||
* @see org.junit.runners.MethodSorters | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface FixMethodOrder { | ||
/** | ||
* Optionally specify <code>value</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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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. | ||
* Defines common {@link MethodSorter} implementations. | ||
*/ | ||
public enum MethodSorters { | ||
/** Sorts the test methods by the method name, in lexicographic order */ | ||
NAME_ASCENDING(MethodSorter.NAME_ASCENDING), | ||
/** Sorts the test methods by the method name, in reverse lexicographic order */ | ||
JVM(null), | ||
/** the default value, 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 | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,95 @@ | ||
package org.junit.internal; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runners.MethodSorters; | ||
import static org.junit.Assert.*; | ||
|
||
public class MethodSorterTest { | ||
private static final String ALPHA= "java.lang.Object alpha(int,double,java.lang.Thread)"; | ||
private static final String BETA= "void beta(int[][])"; | ||
private static final String GAMMA_VOID= "int gamma()"; | ||
private static final String GAMMA_BOOLEAN= "void gamma(boolean)"; | ||
private static final String DELTA= "void delta()"; | ||
private static final String EPSILON= "void epsilon()"; | ||
|
||
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 { | ||
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)); | ||
@Test public void getMethodsNullSorter() throws Exception { | ||
String[] expected= new String[] { EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN }; | ||
assertEquals(Arrays.asList(expected).toString(), declaredMethods(Dummy.class)); | ||
assertEquals("[void testOne()]", declaredMethods(Super.class)); | ||
assertEquals("[void testTwo()]", declaredMethods(Sub.class)); | ||
} | ||
|
||
private static String declaredMethods(Class<?> c) { | ||
return Arrays.toString(MethodSorter.getDeclaredMethods(c)).replace(c.getName() + '.', ""); | ||
|
||
@FixMethodOrder(MethodSorters.DEFAULT) | ||
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() { | ||
String[] expected= new String[] { EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN }; | ||
assertEquals(Arrays.asList(expected).toString(), declaredMethods(DummySortWithDefault.class)); | ||
} | ||
|
||
@FixMethodOrder(MethodSorters.JVM) | ||
private static class DummySortJvm { | ||
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() {} | ||
|
||
@Test public void testSortWithJvm() { | ||
Class<?> clazz= DummySortJvm.class; | ||
String actual= toString(clazz, clazz.getDeclaredMethods()); | ||
|
||
assertEquals(actual, declaredMethods(clazz)); | ||
} | ||
private static class Sub extends Super { | ||
void testTwo() {} | ||
|
||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
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() {} | ||
} | ||
|
||
@Test public void testNameAsc() { | ||
String[] expected= new String[] { ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN }; | ||
assertEquals(Arrays.asList(expected).toString(), declaredMethods(DummySortWithNameAsc.class)); | ||
} | ||
} |
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