01: package org.junit.tests;
02:
03: import static org.junit.Assert.assertEquals;
04: import static org.junit.Assert.assertTrue;
05: import junit.framework.JUnit4TestAdapter;
06: import junit.framework.TestCase;
07: import junit.framework.TestSuite;
08: import org.junit.runner.JUnitCore;
09: import org.junit.runner.RunWith;
10: import org.junit.runners.AllTests;
11:
12: public class AllTestsTest {
13:
14: private static boolean run;
15:
16: public static class OneTest extends TestCase {
17: public void testSomething() {
18: run = true;
19: }
20: }
21:
22: @RunWith(AllTests.class)
23: public static class All {
24: static public junit.framework.Test suite() {
25: TestSuite suite = new TestSuite();
26: suite.addTestSuite(OneTest.class);
27: return suite;
28: }
29: }
30:
31: @org.junit.Test
32: public void ensureTestIsRun() {
33: JUnitCore runner = new JUnitCore();
34: run = false; // Have to explicitly set run here because the runner might independently run OneTest above
35: runner.run(All.class);
36: assertTrue(run);
37: }
38:
39: @org.junit.Test
40: public void correctTestCount() throws Throwable {
41: AllTests tests = new AllTests(All.class);
42: assertEquals(1, tests.testCount());
43: }
44:
45: public static class JUnit4Test {
46: @org.junit.Test
47: public void testSomething() {
48: run = true;
49: }
50: }
51:
52: @RunWith(AllTests.class)
53: public static class AllJUnit4 {
54: static public junit.framework.Test suite() {
55: TestSuite suite = new TestSuite();
56: suite.addTest(new JUnit4TestAdapter(JUnit4Test.class));
57: return suite;
58: }
59: }
60:
61: @org.junit.Test
62: public void correctTestCountAdapted() throws Throwable {
63: AllTests tests = new AllTests(AllJUnit4.class);
64: assertEquals(1, tests.testCount());
65: }
66:
67: @RunWith(AllTests.class)
68: public static class BadSuiteMethod {
69: public static junit.framework.Test suite() {
70: throw new RuntimeException("can't construct");
71: }
72: }
73:
74: @org.junit.Test(expected=RuntimeException.class)
75: public void exceptionThrownWhenSuiteIsBad() throws Throwable {
76: new AllTests(BadSuiteMethod.class);
77: }
78: }
|