Skip to content

Commit 7c2f12c

Browse files
kcooneymarcphilipp
authored andcommitted
Never reorder classes annotated with @FixMethodOrder
Fixes #1637
1 parent 6c5de81 commit 7c2f12c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/main/java/org/junit/runners/ParentRunner.java

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.AfterClass;
2020
import org.junit.BeforeClass;
2121
import org.junit.ClassRule;
22+
import org.junit.FixMethodOrder;
2223
import org.junit.Ignore;
2324
import org.junit.Rule;
2425
import org.junit.internal.AssumptionViolatedException;
@@ -451,6 +452,10 @@ public void filter(Filter filter) throws NoTestsRemainException {
451452
}
452453

453454
public void sort(Sorter sorter) {
455+
if (shouldNotReorder()) {
456+
return;
457+
}
458+
454459
childrenLock.lock();
455460
try {
456461
for (T each : getFilteredChildren()) {
@@ -470,6 +475,10 @@ public void sort(Sorter sorter) {
470475
* @since 4.13
471476
*/
472477
public void order(Orderer orderer) throws InvalidOrderingException {
478+
if (shouldNotReorder()) {
479+
return;
480+
}
481+
473482
childrenLock.lock();
474483
try {
475484
List<T> children = getFilteredChildren();
@@ -504,6 +513,11 @@ public void order(Orderer orderer) throws InvalidOrderingException {
504513
// Private implementation
505514
//
506515

516+
private boolean shouldNotReorder() {
517+
// If the test specifies a specific order, do not reorder.
518+
return getDescription().getAnnotation(FixMethodOrder.class) != null;
519+
}
520+
507521
private void validate() throws InitializationError {
508522
List<Throwable> errors = new ArrayList<Throwable>();
509523
collectInitializationErrors(errors);

src/test/java/org/junit/tests/manipulation/OrderableTest.java

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertEquals;
44
import junit.framework.JUnit4TestAdapter;
55
import org.junit.Before;
6+
import org.junit.FixMethodOrder;
67
import org.junit.Test;
78
import org.junit.experimental.runners.Enclosed;
89
import org.junit.runner.Description;
@@ -16,6 +17,7 @@
1617
import org.junit.runner.manipulation.Sorter;
1718
import org.junit.runner.notification.RunNotifier;
1819
import org.junit.runners.BlockJUnit4ClassRunner;
20+
import org.junit.runners.MethodSorters;
1921

2022
@RunWith(Enclosed.class)
2123
public class OrderableTest {
@@ -40,6 +42,24 @@ public void c() {
4042
}
4143
}
4244

45+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46+
public static class DoNotOrderMe {
47+
@Test
48+
public void a() {
49+
log += "a";
50+
}
51+
52+
@Test
53+
public void b() {
54+
log += "b";
55+
}
56+
57+
@Test
58+
public void c() {
59+
log += "c";
60+
}
61+
}
62+
4363
@Before
4464
public void resetLog() {
4565
log = "";
@@ -62,6 +82,15 @@ public void orderingBackwardWorksOnTestClassRunner() {
6282
new JUnitCore().run(backward);
6383
assertEquals("cba", log);
6484
}
85+
86+
@Test
87+
public void orderingBackwardDoesNothingOnTestClassRunnerWithFixMethodOrder() {
88+
Request backward = Request.aClass(DoNotOrderMe.class).orderWith(
89+
new ReverseAlphanumericOrdering());
90+
91+
new JUnitCore().run(backward);
92+
assertEquals("abc", log);
93+
}
6594

6695
@RunWith(Enclosed.class)
6796
public static class Enclosing {

src/test/java/org/junit/tests/manipulation/SortableTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import junit.framework.JUnit4TestAdapter;
99
import org.junit.Before;
10+
import org.junit.FixMethodOrder;
1011
import org.junit.Test;
1112
import org.junit.experimental.runners.Enclosed;
1213
import org.junit.runner.Description;
@@ -19,6 +20,7 @@
1920
import org.junit.runner.manipulation.Sorter;
2021
import org.junit.runner.notification.RunNotifier;
2122
import org.junit.runners.BlockJUnit4ClassRunner;
23+
import org.junit.runners.MethodSorters;
2224

2325
@RunWith(Enclosed.class)
2426
public class SortableTest {
@@ -50,6 +52,24 @@ public void c() {
5052
}
5153
}
5254

55+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56+
public static class DoNotSortMe {
57+
@Test
58+
public void a() {
59+
log += "a";
60+
}
61+
62+
@Test
63+
public void b() {
64+
log += "b";
65+
}
66+
67+
@Test
68+
public void c() {
69+
log += "c";
70+
}
71+
}
72+
5373
@Before
5474
public void resetLog() {
5575
log = "";
@@ -71,6 +91,14 @@ public void sortingBackwardWorksOnTestClassRunner() {
7191
assertEquals("cba", log);
7292
}
7393

94+
@Test
95+
public void sortingBackwardDoesNothingOnTestClassRunnerWithFixMethodOrder() {
96+
Request backward = Request.aClass(DoNotSortMe.class).sortWith(backward());
97+
98+
new JUnitCore().run(backward);
99+
assertEquals("abc", log);
100+
}
101+
74102
@RunWith(Enclosed.class)
75103
public static class Enclosing {
76104
public static class A {

0 commit comments

Comments
 (0)