File tree 3 files changed +71
-0
lines changed
main/java/org/junit/runners
test/java/org/junit/tests/manipulation
3 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 19
19
import org .junit .AfterClass ;
20
20
import org .junit .BeforeClass ;
21
21
import org .junit .ClassRule ;
22
+ import org .junit .FixMethodOrder ;
22
23
import org .junit .Ignore ;
23
24
import org .junit .Rule ;
24
25
import org .junit .internal .AssumptionViolatedException ;
@@ -451,6 +452,10 @@ public void filter(Filter filter) throws NoTestsRemainException {
451
452
}
452
453
453
454
public void sort (Sorter sorter ) {
455
+ if (shouldNotReorder ()) {
456
+ return ;
457
+ }
458
+
454
459
childrenLock .lock ();
455
460
try {
456
461
for (T each : getFilteredChildren ()) {
@@ -470,6 +475,10 @@ public void sort(Sorter sorter) {
470
475
* @since 4.13
471
476
*/
472
477
public void order (Orderer orderer ) throws InvalidOrderingException {
478
+ if (shouldNotReorder ()) {
479
+ return ;
480
+ }
481
+
473
482
childrenLock .lock ();
474
483
try {
475
484
List <T > children = getFilteredChildren ();
@@ -504,6 +513,11 @@ public void order(Orderer orderer) throws InvalidOrderingException {
504
513
// Private implementation
505
514
//
506
515
516
+ private boolean shouldNotReorder () {
517
+ // If the test specifies a specific order, do not reorder.
518
+ return getDescription ().getAnnotation (FixMethodOrder .class ) != null ;
519
+ }
520
+
507
521
private void validate () throws InitializationError {
508
522
List <Throwable > errors = new ArrayList <Throwable >();
509
523
collectInitializationErrors (errors );
Original file line number Diff line number Diff line change 3
3
import static org .junit .Assert .assertEquals ;
4
4
import junit .framework .JUnit4TestAdapter ;
5
5
import org .junit .Before ;
6
+ import org .junit .FixMethodOrder ;
6
7
import org .junit .Test ;
7
8
import org .junit .experimental .runners .Enclosed ;
8
9
import org .junit .runner .Description ;
16
17
import org .junit .runner .manipulation .Sorter ;
17
18
import org .junit .runner .notification .RunNotifier ;
18
19
import org .junit .runners .BlockJUnit4ClassRunner ;
20
+ import org .junit .runners .MethodSorters ;
19
21
20
22
@ RunWith (Enclosed .class )
21
23
public class OrderableTest {
@@ -40,6 +42,24 @@ public void c() {
40
42
}
41
43
}
42
44
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
+
43
63
@ Before
44
64
public void resetLog () {
45
65
log = "" ;
@@ -62,6 +82,15 @@ public void orderingBackwardWorksOnTestClassRunner() {
62
82
new JUnitCore ().run (backward );
63
83
assertEquals ("cba" , log );
64
84
}
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
+ }
65
94
66
95
@ RunWith (Enclosed .class )
67
96
public static class Enclosing {
Original file line number Diff line number Diff line change 7
7
8
8
import junit .framework .JUnit4TestAdapter ;
9
9
import org .junit .Before ;
10
+ import org .junit .FixMethodOrder ;
10
11
import org .junit .Test ;
11
12
import org .junit .experimental .runners .Enclosed ;
12
13
import org .junit .runner .Description ;
19
20
import org .junit .runner .manipulation .Sorter ;
20
21
import org .junit .runner .notification .RunNotifier ;
21
22
import org .junit .runners .BlockJUnit4ClassRunner ;
23
+ import org .junit .runners .MethodSorters ;
22
24
23
25
@ RunWith (Enclosed .class )
24
26
public class SortableTest {
@@ -50,6 +52,24 @@ public void c() {
50
52
}
51
53
}
52
54
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
+
53
73
@ Before
54
74
public void resetLog () {
55
75
log = "" ;
@@ -71,6 +91,14 @@ public void sortingBackwardWorksOnTestClassRunner() {
71
91
assertEquals ("cba" , log );
72
92
}
73
93
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
+
74
102
@ RunWith (Enclosed .class )
75
103
public static class Enclosing {
76
104
public static class A {
You can’t perform that action at this time.
0 commit comments