Skip to content

Commit 877750a

Browse files
committed
Extract sample test classes from StackTracesTest
Follow Kevin's idea from ead2fe7 up that sample test classes are stored in a specific container.
1 parent ba3799f commit 877750a

File tree

4 files changed

+176
-178
lines changed

4 files changed

+176
-178
lines changed

src/main/java/org/junit/internal/Throwables.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ public final State processStackTraceElement(StackTraceElement element) {
232232
"org.junit.runners.",
233233
"org.junit.experimental.runners.",
234234
"org.junit.internal.",
235-
"junit.",
235+
"junit.extensions",
236+
"junit.framework",
237+
"junit.runner",
238+
"junit.textui",
236239
};
237240

238241
private static final String[] TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES = {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package junit.tests;
2+
3+
import junit.framework.TestCase;
4+
5+
/**
6+
* Container for sample JUnit3-style tests used in integration tests.
7+
*/
8+
public class SampleJUnit3Tests {
9+
10+
public static class TestWithOneThrowingTestMethod extends TestCase {
11+
12+
public void testAlwaysThrows() {
13+
new FakeClassUnderTest().throwsExceptionWithoutCause();
14+
}
15+
}
16+
17+
public static class TestWithThrowingSetUpMethod extends TestCase {
18+
19+
@Override
20+
protected void setUp() throws Exception {
21+
super.setUp();
22+
new FakeClassUnderTest().throwsExceptionWithoutCause();
23+
}
24+
25+
public void testAlwaysPasses() {
26+
}
27+
}
28+
29+
private static class FakeClassUnderTest {
30+
31+
public void throwsExceptionWithCause() {
32+
doThrowExceptionWithCause();
33+
}
34+
35+
public void throwsExceptionWithoutCause() {
36+
doThrowExceptionWithoutCause();
37+
}
38+
39+
private void doThrowExceptionWithCause() {
40+
try {
41+
throwsExceptionWithoutCause();
42+
} catch (Exception e) {
43+
throw new RuntimeException("outer", e);
44+
}
45+
}
46+
47+
private void doThrowExceptionWithoutCause() {
48+
throw new RuntimeException("cause");
49+
}
50+
}
51+
}

src/test/java/org/junit/internal/StackTracesTest.java

Lines changed: 33 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,26 @@
66
import static org.junit.Assert.fail;
77
import static org.junit.Assume.assumeTrue;
88

9-
import java.lang.reflect.Method;
109
import java.util.concurrent.Callable;
1110
import java.util.concurrent.ExecutionException;
1211
import java.util.concurrent.ExecutorService;
1312
import java.util.concurrent.Executors;
1413
import java.util.concurrent.Future;
1514
import java.util.regex.Pattern;
1615

17-
import junit.framework.TestCase;
16+
import junit.tests.SampleJUnit3Tests;
1817
import org.hamcrest.CoreMatchers;
1918
import org.hamcrest.Description;
2019
import org.hamcrest.Matcher;
2120
import org.hamcrest.StringDescription;
2221
import org.hamcrest.TypeSafeMatcher;
2322
import org.junit.AfterClass;
24-
import org.junit.Before;
2523
import org.junit.BeforeClass;
26-
import org.junit.ClassRule;
27-
import org.junit.Rule;
2824
import org.junit.Test;
29-
import org.junit.rules.MethodRule;
30-
import org.junit.rules.TestRule;
3125
import org.junit.runner.JUnitCore;
3226
import org.junit.runner.Result;
3327
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.*;
3629

3730
public class StackTracesTest {
3831
private static final String EOL = System.getProperty("line.separator", "\n");
@@ -58,9 +51,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethod() {
5851

5952
assertHasTrimmedTrace(failure,
6053
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"));
6457
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
6558
}
6659

@@ -73,14 +66,14 @@ public void getTrimmedStackForJUnit4TestFailingInTestMethodWithCause() {
7366

7467
assertHasTrimmedTrace(failure,
7568
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"),
7972
framesTrimmed(),
8073
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"),
8477
framesInCommon());
8578
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
8679
}
@@ -94,39 +87,39 @@ public void getTrimmedStackForJUnit4TestFailingInBeforeMethod() {
9487

9588
assertHasTrimmedTrace(failure,
9689
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"));
10093
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
10194
}
10295

10396
@Test
10497
public void getTrimmedStackForJUnit3TestFailingInTestMethod() {
105-
Result result = runTest(JUnit3TestWithOneThrowingTestMethod.class);
98+
Result result = runTest(SampleJUnit3Tests.TestWithOneThrowingTestMethod.class);
10699
assertEquals("Should run the test", 1, result.getRunCount());
107100
assertEquals("One test should fail", 1, result.getFailureCount());
108101
Failure failure = result.getFailures().get(0);
109102

110103
assertHasTrimmedTrace(failure,
111104
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"));
115108
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
116109
}
117110

118111
@Test
119112
public void getTrimmedStackForJUnit3TestFailingInSetupMethod() {
120-
Result result = runTest(JUnit3TestWithThrowingSetUpMethod.class);
113+
Result result = runTest(SampleJUnit3Tests.TestWithThrowingSetUpMethod.class);
121114
assertEquals("Should run the test", 1, result.getRunCount());
122115
assertEquals("One test should fail", 1, result.getFailureCount());
123116
Failure failure = result.getFailures().get(0);
124117

125118
assertHasTrimmedTrace(failure,
126119
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"));
130123
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
131124
}
132125

@@ -139,9 +132,9 @@ public void getTrimmedStackForJUnit4TestFailingInTestRule() {
139132

140133
assertHasTrimmedTrace(failure,
141134
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"));
145138
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
146139
}
147140

@@ -154,9 +147,9 @@ public void getTrimmedStackForJUnit4TestFailingInClassRule() {
154147

155148
assertHasTrimmedTrace(failure,
156149
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"));
160153
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
161154
}
162155

@@ -169,9 +162,9 @@ public void getTrimmedStackForJUnit4TestFailingInMethodRule() {
169162

170163
assertHasTrimmedTrace(failure,
171164
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"));
175168
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
176169
}
177170

@@ -185,9 +178,9 @@ public void getTrimmedStackWithSuppressedExceptions() {
185178

186179
assertHasTrimmedTrace(failure,
187180
message("java.lang.RuntimeException: error"),
188-
at("org.junit.internal.StackTracesTest$TestWithSuppressedException.alwaysThrows"),
181+
at("org.junit.tests.SampleJUnit4Tests$TestWithSuppressedException.alwaysThrows"),
189182
message("\tSuppressed: java.lang.RuntimeException: suppressed"),
190-
at("org.junit.internal.StackTracesTest$TestWithSuppressedException.alwaysThrows"),
183+
at("org.junit.tests.SampleJUnit4Tests$TestWithSuppressedException.alwaysThrows"),
191184
framesInCommon());
192185
assertNotEquals(failure.getTrace(), failure.getTrimmedTrace());
193186
}
@@ -336,141 +329,4 @@ private static void assertHasTrimmedTrace(Failure failure, StringMatcher... matc
336329
fail("Missing line in trimmed trace: " + description.toString());
337330
}
338331
}
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-
}
476332
}

0 commit comments

Comments
 (0)