6
6
import static org .junit .Assert .fail ;
7
7
import static org .junit .Assume .assumeTrue ;
8
8
9
- import java .lang .reflect .Method ;
10
9
import java .util .concurrent .Callable ;
11
10
import java .util .concurrent .ExecutionException ;
12
11
import java .util .concurrent .ExecutorService ;
13
12
import java .util .concurrent .Executors ;
14
13
import java .util .concurrent .Future ;
15
14
import java .util .regex .Pattern ;
16
15
17
- import junit .framework . TestCase ;
16
+ import junit .tests . SampleJUnit3Tests ;
18
17
import org .hamcrest .CoreMatchers ;
19
18
import org .hamcrest .Description ;
20
19
import org .hamcrest .Matcher ;
21
20
import org .hamcrest .StringDescription ;
22
21
import org .hamcrest .TypeSafeMatcher ;
23
22
import org .junit .AfterClass ;
24
- import org .junit .Before ;
25
23
import org .junit .BeforeClass ;
26
- import org .junit .ClassRule ;
27
- import org .junit .Rule ;
28
24
import org .junit .Test ;
29
- import org .junit .rules .MethodRule ;
30
- import org .junit .rules .TestRule ;
31
25
import org .junit .runner .JUnitCore ;
32
26
import org .junit .runner .Result ;
33
27
import org .junit .runner .notification .Failure ;
34
- import org .junit .runners .model .FrameworkMethod ;
35
- import org .junit .runners .model .Statement ;
28
+ import org .junit .tests .SampleJUnit4Tests .*;
36
29
37
30
public class StackTracesTest {
38
31
private static final String EOL = System .getProperty ("line.separator" , "\n " );
@@ -58,9 +51,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethod() {
58
51
59
52
assertHasTrimmedTrace (failure ,
60
53
message ("java.lang.RuntimeException: cause" ),
61
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
62
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
63
- at ("org.junit.internal.StackTracesTest $TestWithOneThrowingTestMethod.alwaysThrows" ));
54
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
55
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
56
+ at ("org.junit.tests.SampleJUnit4Tests $TestWithOneThrowingTestMethod.alwaysThrows" ));
64
57
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
65
58
}
66
59
@@ -73,14 +66,14 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethodWithCause() {
73
66
74
67
assertHasTrimmedTrace (failure ,
75
68
message ("java.lang.RuntimeException: outer" ),
76
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithCause" ),
77
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithCause" ),
78
- at ("org.junit.internal.StackTracesTest $TestWithOneThrowingTestMethodWithCause.alwaysThrows" ),
69
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithCause" ),
70
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithCause" ),
71
+ at ("org.junit.tests.SampleJUnit4Tests $TestWithOneThrowingTestMethodWithCause.alwaysThrows" ),
79
72
framesTrimmed (),
80
73
message ("Caused by: java.lang.RuntimeException: cause" ),
81
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
82
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
83
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithCause" ),
74
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
75
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
76
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithCause" ),
84
77
framesInCommon ());
85
78
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
86
79
}
@@ -94,39 +87,39 @@ public void getTrimmedStackForJUnit4TestFailingInBeforeMethod() {
94
87
95
88
assertHasTrimmedTrace (failure ,
96
89
message ("java.lang.RuntimeException: cause" ),
97
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
98
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
99
- at ("org.junit.internal.StackTracesTest $TestWithThrowingBeforeMethod.alwaysThrows" ));
90
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
91
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
92
+ at ("org.junit.tests.SampleJUnit4Tests $TestWithThrowingBeforeMethod.alwaysThrows" ));
100
93
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
101
94
}
102
95
103
96
@ Test
104
97
public void getTrimmedStackForJUnit3TestFailingInTestMethod () {
105
- Result result = runTest (JUnit3TestWithOneThrowingTestMethod .class );
98
+ Result result = runTest (SampleJUnit3Tests . TestWithOneThrowingTestMethod .class );
106
99
assertEquals ("Should run the test" , 1 , result .getRunCount ());
107
100
assertEquals ("One test should fail" , 1 , result .getFailureCount ());
108
101
Failure failure = result .getFailures ().get (0 );
109
102
110
103
assertHasTrimmedTrace (failure ,
111
104
message ("java.lang.RuntimeException: cause" ),
112
- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
113
- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
114
- at ("org. junit.internal.StackTracesTest$JUnit3TestWithOneThrowingTestMethod .testAlwaysThrows" ));
105
+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
106
+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
107
+ at ("junit.tests.SampleJUnit3Tests$TestWithOneThrowingTestMethod .testAlwaysThrows" ));
115
108
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
116
109
}
117
110
118
111
@ Test
119
112
public void getTrimmedStackForJUnit3TestFailingInSetupMethod () {
120
- Result result = runTest (JUnit3TestWithThrowingSetUpMethod .class );
113
+ Result result = runTest (SampleJUnit3Tests . TestWithThrowingSetUpMethod .class );
121
114
assertEquals ("Should run the test" , 1 , result .getRunCount ());
122
115
assertEquals ("One test should fail" , 1 , result .getFailureCount ());
123
116
Failure failure = result .getFailures ().get (0 );
124
117
125
118
assertHasTrimmedTrace (failure ,
126
119
message ("java.lang.RuntimeException: cause" ),
127
- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
128
- at ("org. junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
129
- at ("org. junit.internal.StackTracesTest$JUnit3TestWithThrowingSetUpMethod .setUp" ));
120
+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
121
+ at ("junit.tests.SampleJUnit3Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
122
+ at ("junit.tests.SampleJUnit3Tests$TestWithThrowingSetUpMethod .setUp" ));
130
123
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
131
124
}
132
125
@@ -139,9 +132,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestRule() {
139
132
140
133
assertHasTrimmedTrace (failure ,
141
134
message ("java.lang.RuntimeException: cause" ),
142
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
143
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
144
- at ("org.junit.internal.StackTracesTest $ThrowingTestRule.apply" ));
135
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
136
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
137
+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingTestRule.apply" ));
145
138
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
146
139
}
147
140
@@ -154,9 +147,9 @@ public void getTrimmedStackForJUnit4TestFailingInClassRule() {
154
147
155
148
assertHasTrimmedTrace (failure ,
156
149
message ("java.lang.RuntimeException: cause" ),
157
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
158
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
159
- at ("org.junit.internal.StackTracesTest $ThrowingTestRule.apply" ));
150
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
151
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
152
+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingTestRule.apply" ));
160
153
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
161
154
}
162
155
@@ -169,9 +162,9 @@ public void getTrimmedStackForJUnit4TestFailingInMethodRule() {
169
162
170
163
assertHasTrimmedTrace (failure ,
171
164
message ("java.lang.RuntimeException: cause" ),
172
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
173
- at ("org.junit.internal.StackTracesTest $FakeClassUnderTest.throwsExceptionWithoutCause" ),
174
- at ("org.junit.internal.StackTracesTest $ThrowingMethodRule.apply" ));
165
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.doThrowExceptionWithoutCause" ),
166
+ at ("org.junit.tests.SampleJUnit4Tests $FakeClassUnderTest.throwsExceptionWithoutCause" ),
167
+ at ("org.junit.tests.SampleJUnit4Tests $ThrowingMethodRule.apply" ));
175
168
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
176
169
}
177
170
@@ -185,9 +178,9 @@ public void getTrimmedStackWithSuppressedExceptions() {
185
178
186
179
assertHasTrimmedTrace (failure ,
187
180
message ("java.lang.RuntimeException: error" ),
188
- at ("org.junit.internal.StackTracesTest $TestWithSuppressedException.alwaysThrows" ),
181
+ at ("org.junit.tests.SampleJUnit4Tests $TestWithSuppressedException.alwaysThrows" ),
189
182
message ("\t Suppressed: java.lang.RuntimeException: suppressed" ),
190
- at ("org.junit.internal.StackTracesTest $TestWithSuppressedException.alwaysThrows" ),
183
+ at ("org.junit.tests.SampleJUnit4Tests $TestWithSuppressedException.alwaysThrows" ),
191
184
framesInCommon ());
192
185
assertNotEquals (failure .getTrace (), failure .getTrimmedTrace ());
193
186
}
@@ -336,141 +329,4 @@ private static void assertHasTrimmedTrace(Failure failure, StringMatcher... matc
336
329
fail ("Missing line in trimmed trace: " + description .toString ());
337
330
}
338
331
}
339
-
340
- public static class TestWithOneThrowingTestMethod {
341
-
342
- @ Test
343
- public void alwaysThrows () {
344
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
345
- }
346
- }
347
-
348
- public static class JUnit3TestWithOneThrowingTestMethod extends TestCase {
349
-
350
- public void testAlwaysThrows () {
351
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
352
- }
353
- }
354
-
355
- public static class TestWithOneThrowingTestMethodWithCause {
356
-
357
- @ Test
358
- public void alwaysThrows () {
359
- new FakeClassUnderTest ().throwsExceptionWithCause ();
360
- }
361
- }
362
-
363
- public static class TestWithThrowingBeforeMethod {
364
-
365
- @ Before
366
- public void alwaysThrows () {
367
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
368
- }
369
-
370
- @ Test
371
- public void alwaysPasses () {
372
- }
373
- }
374
-
375
- public static class JUnit3TestWithThrowingSetUpMethod extends TestCase {
376
-
377
- @ Override
378
- protected void setUp () throws Exception {
379
- super .setUp ();
380
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
381
- }
382
-
383
- public void testAlwaysPasses () {
384
- }
385
- }
386
-
387
- public static class ThrowingTestRule implements TestRule {
388
-
389
- public Statement apply (
390
- Statement base , org .junit .runner .Description description ) {
391
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
392
- return base ;
393
- }
394
- }
395
-
396
- public static class TestWithThrowingTestRule {
397
-
398
- @ Rule
399
- public final TestRule rule = new ThrowingTestRule ();
400
-
401
- @ Test
402
- public void alwaysPasses () {
403
- }
404
- }
405
-
406
- public static class TestWithThrowingClassRule {
407
-
408
- @ ClassRule
409
- public static final TestRule rule = new ThrowingTestRule ();
410
-
411
- @ Test
412
- public void alwaysPasses () {
413
- }
414
- }
415
-
416
- public static class ThrowingMethodRule implements MethodRule {
417
-
418
- public Statement apply (
419
- Statement base , FrameworkMethod method , Object target ) {
420
- new FakeClassUnderTest ().throwsExceptionWithoutCause ();
421
- return base ;
422
- }
423
- }
424
-
425
- public static class TestWithThrowingMethodRule {
426
-
427
- @ Rule
428
- public final ThrowingMethodRule rule = new ThrowingMethodRule ();
429
-
430
- @ Test
431
- public void alwaysPasses () {
432
- }
433
- }
434
-
435
- private static class FakeClassUnderTest {
436
-
437
- public void throwsExceptionWithCause () {
438
- doThrowExceptionWithCause ();
439
- }
440
-
441
- public void throwsExceptionWithoutCause () {
442
- doThrowExceptionWithoutCause ();
443
- }
444
-
445
- private void doThrowExceptionWithCause () {
446
- try {
447
- throwsExceptionWithoutCause ();
448
- } catch (Exception e ) {
449
- throw new RuntimeException ("outer" , e );
450
- }
451
- }
452
-
453
- private void doThrowExceptionWithoutCause () {
454
- throw new RuntimeException ("cause" );
455
- }
456
- }
457
-
458
- public static class TestWithSuppressedException {
459
- static final Method addSuppressed = initAddSuppressed ();
460
-
461
- static Method initAddSuppressed () {
462
- try {
463
- return Throwable .class .getMethod ("addSuppressed" , Throwable .class );
464
- } catch (Throwable e ) {
465
- return null ;
466
- }
467
- }
468
-
469
- @ Test
470
- public void alwaysThrows () throws Exception {
471
- final RuntimeException exception = new RuntimeException ("error" );
472
- addSuppressed .invoke (exception , new RuntimeException ("suppressed" ));
473
- throw exception ;
474
- }
475
- }
476
332
}
0 commit comments