Skip to content

MethodSorter METHOD_ORDER allowing to specify the execution order of test methods #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/junit/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ private None() {
* </p>
*/
long timeout() default 0L;

/**
* Optionally specify the execution order of a test method. Methods are executed from the lowest <code>order</code>
* to the highest.
*/
int order() default 0;
}
12 changes: 12 additions & 0 deletions src/main/java/org/junit/internal/MethodSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Comparator;

import org.junit.FixMethodOrder;
import org.junit.Test;

public class MethodSorter {
/**
Expand Down Expand Up @@ -34,6 +35,17 @@ public int compare(Method m1, Method m2) {
}
};

/**
* Sort order using {@link Test#order()} attributes
*/
public static Comparator<Method> METHOD_ORDER = new Comparator<Method>() {
public int compare(Method m1, Method m2) {
final Test t1 = m1.getAnnotation(Test.class);
final Test t2 = m2.getAnnotation(Test.class);
return (t1 != null ? t1.order() : 0) - (t2 != null ? t2.order() : 0);
}
};

/**
* Gets declared methods of a class in a predictable order, unless @FixMethodOrder(MethodSorters.JVM) is specified.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/junit/runners/MethodSorters.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Method;
import java.util.Comparator;

import org.junit.Test;
import org.junit.internal.MethodSorter;

/**
Expand All @@ -18,6 +19,11 @@ public enum MethodSorters {
*/
NAME_ASCENDING(MethodSorter.NAME_ASCENDING),

/**
* Sorts the test methods using {@link Test#order()} attributes
*/
METHOD_ORDER(MethodSorter.METHOD_ORDER),

/**
* Leaves the test methods in the order returned by the JVM.
* Note that the order from the JVM may vary from run to run
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/junit/internal/MethodSorterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,40 @@ public void testAscendingMethodSorter() {
List<String> actual = getDeclaredMethodNames(DummySortWithNameAsc.class);
assertEquals(expected, actual);
}

@FixMethodOrder(MethodSorters.METHOD_ORDER)
static class DummySortWithMethodOrder {
@Test(order=1)
Object alpha(int i, double d, Thread t) {
return null;
}

@Test(order=2)
void beta(int[][] x) {
}

@Test(order=5)
int gamma() {
return 0;
}

@Test(order=6)
void gamma(boolean b) {
}

@Test(order=3)
void delta() {
}

@Test(order=4)
void epsilon() {
}
}

@Test
public void testMethodOrderSorter() {
List<String> expected = Arrays.asList(ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN);
List<String> actual = getDeclaredMethodNames(DummySortWithMethodOrder.class);
assertEquals(expected, actual);
}
}