Skip to content

Commit

Permalink
Never reorder classes annotated with @FixMethodOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
kcooney committed Nov 21, 2019
1 parent 6c5de81 commit 7d102ae
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.internal.AssumptionViolatedException;
Expand Down Expand Up @@ -451,6 +452,10 @@ public void filter(Filter filter) throws NoTestsRemainException {
}

public void sort(Sorter sorter) {
if (shouldNotReorder()) {
return;
}

childrenLock.lock();
try {
for (T each : getFilteredChildren()) {
Expand All @@ -470,6 +475,10 @@ public void sort(Sorter sorter) {
* @since 4.13
*/
public void order(Orderer orderer) throws InvalidOrderingException {
if (shouldNotReorder()) {
return;
}

childrenLock.lock();
try {
List<T> children = getFilteredChildren();
Expand Down Expand Up @@ -504,6 +513,11 @@ public void order(Orderer orderer) throws InvalidOrderingException {
// Private implementation
//

private boolean shouldNotReorder() {
// If the test specifies a specific order, do not reorder.
return getDescription().getAnnotation(FixMethodOrder.class) != null;
}

private void validate() throws InitializationError {
List<Throwable> errors = new ArrayList<Throwable>();
collectInitializationErrors(errors);
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/junit/tests/manipulation/OrderableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.Description;
Expand All @@ -16,6 +17,7 @@
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.MethodSorters;

@RunWith(Enclosed.class)
public class OrderableTest {
Expand All @@ -40,6 +42,24 @@ public void c() {
}
}

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class DoNotOrderMe {
@Test
public void a() {
log += "a";
}

@Test
public void b() {
log += "b";
}

@Test
public void c() {
log += "c";
}
}

@Before
public void resetLog() {
log = "";
Expand All @@ -62,6 +82,15 @@ public void orderingBackwardWorksOnTestClassRunner() {
new JUnitCore().run(backward);
assertEquals("cba", log);
}

@Test
public void orderingBackwardDoesNothingOnTestClassRunnerWithFixMethodOrder() {
Request backward = Request.aClass(DoNotOrderMe.class).orderWith(
new ReverseAlphanumericOrdering());

new JUnitCore().run(backward);
assertEquals("abc", log);
}

@RunWith(Enclosed.class)
public static class Enclosing {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/junit/tests/manipulation/SortableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import junit.framework.JUnit4TestAdapter;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.Description;
Expand All @@ -19,6 +20,7 @@
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.MethodSorters;

@RunWith(Enclosed.class)
public class SortableTest {
Expand Down Expand Up @@ -50,6 +52,24 @@ public void c() {
}
}

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class DoNotSortMe {
@Test
public void a() {
log += "a";
}

@Test
public void b() {
log += "b";
}

@Test
public void c() {
log += "c";
}
}

@Before
public void resetLog() {
log = "";
Expand All @@ -71,6 +91,14 @@ public void sortingBackwardWorksOnTestClassRunner() {
assertEquals("cba", log);
}

@Test
public void sortingBackwardDoesNothingOnTestClassRunnerWithFixMethodOrder() {
Request backward = Request.aClass(DoNotSortMe.class).sortWith(backward());

new JUnitCore().run(backward);
assertEquals("abc", log);
}

@RunWith(Enclosed.class)
public static class Enclosing {
public static class A {
Expand Down

0 comments on commit 7d102ae

Please sign in to comment.