summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/junit3compatibility/AllTestsTest.java
blob: d9076e3aa0f6ac437d184ceef85c1a0dfbec3512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.junit.tests.junit3compatibility;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;
import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runners.AllTests;

public class AllTestsTest {
	
	private static boolean run;
	
	public static class OneTest extends TestCase {
		public void testSomething() {
			run= true;
		}
	}
	
	@RunWith(AllTests.class)
	public static class All {
		static public junit.framework.Test suite() {
			TestSuite suite= new TestSuite();
			suite.addTestSuite(OneTest.class);
			return suite;
		}
	}
	
	@org.junit.Test public void ensureTestIsRun() {
		JUnitCore runner= new JUnitCore();
		run= false; // Have to explicitly set run here because the runner might independently run OneTest above
		runner.run(All.class);
		assertTrue(run);
	}
	
	@org.junit.Test public void correctTestCount() throws Throwable {
		AllTests tests= new AllTests(All.class);
		assertEquals(1, tests.testCount());
	}
	
	@org.junit.Test public void someUsefulDescription() throws Throwable {
		AllTests tests= new AllTests(All.class);
		assertThat(tests.getDescription().toString(), containsString("OneTest"));
	}
	
	public static class JUnit4Test {
		@org.junit.Test public void testSomething() {
			run= true;
		}
	}
	
	@RunWith(AllTests.class)
	public static class AllJUnit4 {
		static public junit.framework.Test suite() {
			TestSuite suite= new TestSuite();
			suite.addTest(new JUnit4TestAdapter(JUnit4Test.class));
			return suite;
		}
	}

	@org.junit.Test public void correctTestCountAdapted() throws Throwable {
		AllTests tests= new AllTests(AllJUnit4.class);
		assertEquals(1, tests.testCount());
	}
	
	@RunWith(AllTests.class)
	public static class BadSuiteMethod {
		public static junit.framework.Test suite() {
			throw new RuntimeException("can't construct");
		}
	}

	@org.junit.Test(expected= RuntimeException.class)
	public void exceptionThrownWhenSuiteIsBad() throws Throwable {
		new AllTests(BadSuiteMethod.class);
	}
}