1 package org.junit.tests.junit3compatibility;
2
3 import static org.hamcrest.CoreMatchers.containsString;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertThat;
6 import static org.junit.Assert.assertTrue;
7
8 import junit.framework.JUnit4TestAdapter;
9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11 import org.junit.runner.JUnitCore;
12 import org.junit.runner.RunWith;
13 import org.junit.runners.AllTests;
14
15 public class AllTestsTest {
16
17 private static boolean run;
18
19 public static class OneTest extends TestCase {
20 public void testSomething() {
21 run = true;
22 }
23 }
24
25 @RunWith(AllTests.class)
26 public static class All {
27 static public junit.framework.Test suite() {
28 TestSuite suite = new TestSuite();
29 suite.addTestSuite(OneTest.class);
30 return suite;
31 }
32 }
33
34 @org.junit.Test
35 public void ensureTestIsRun() {
36 JUnitCore runner = new JUnitCore();
37 run = false;
38 runner.run(All.class);
39 assertTrue(run);
40 }
41
42 @org.junit.Test
43 public void correctTestCount() throws Throwable {
44 AllTests tests = new AllTests(All.class);
45 assertEquals(1, tests.testCount());
46 }
47
48 @org.junit.Test
49 public void someUsefulDescription() throws Throwable {
50 AllTests tests = new AllTests(All.class);
51 assertThat(tests.getDescription().toString(), containsString("OneTest"));
52 }
53
54 public static class JUnit4Test {
55 @org.junit.Test
56 public void testSomething() {
57 run = true;
58 }
59 }
60
61 @RunWith(AllTests.class)
62 public static class AllJUnit4 {
63 static public junit.framework.Test suite() {
64 TestSuite suite = new TestSuite();
65 suite.addTest(new JUnit4TestAdapter(JUnit4Test.class));
66 return suite;
67 }
68 }
69
70 @org.junit.Test
71 public void correctTestCountAdapted() throws Throwable {
72 AllTests tests = new AllTests(AllJUnit4.class);
73 assertEquals(1, tests.testCount());
74 }
75
76 @RunWith(AllTests.class)
77 public static class BadSuiteMethod {
78 public static junit.framework.Test suite() {
79 throw new RuntimeException("can't construct");
80 }
81 }
82
83 @org.junit.Test(expected = RuntimeException.class)
84 public void exceptionThrownWhenSuiteIsBad() throws Throwable {
85 new AllTests(BadSuiteMethod.class);
86 }
87 }