1 package org.junit.tests.running.classes;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import org.junit.Test;
7 import org.junit.runner.Description;
8 import org.junit.runner.JUnitCore;
9 import org.junit.runner.RunWith;
10 import org.junit.runner.Runner;
11 import org.junit.runner.notification.RunNotifier;
12
13 public class RunWithTest {
14
15 private static String log;
16
17 public static class ExampleRunner extends Runner {
18 public ExampleRunner(Class<?> klass) {
19 log += "initialize";
20 }
21
22 @Override
23 public void run(RunNotifier notifier) {
24 log += "run";
25 }
26
27 @Override
28 public int testCount() {
29 log += "count";
30 return 0;
31 }
32
33 @Override
34 public Description getDescription() {
35 log += "plan";
36 return Description.createSuiteDescription("example");
37 }
38 }
39
40 @RunWith(ExampleRunner.class)
41 public static class ExampleTest {
42 }
43
44 @Test
45 public void run() {
46 log = "";
47
48 JUnitCore.runClasses(ExampleTest.class);
49 assertTrue(log.contains("plan"));
50 assertTrue(log.contains("initialize"));
51 assertTrue(log.contains("run"));
52 }
53
54 public static class SubExampleTest extends ExampleTest {
55 }
56
57 @Test
58 public void runWithExtendsToSubclasses() {
59 log = "";
60
61 JUnitCore.runClasses(SubExampleTest.class);
62 assertTrue(log.contains("run"));
63 }
64
65 public static class BadRunner extends Runner {
66 @Override
67 public Description getDescription() {
68 return null;
69 }
70
71 @Override
72 public void run(RunNotifier notifier) {
73
74 }
75 }
76
77 @RunWith(BadRunner.class)
78 public static class Empty {
79 }
80
81 @Test
82 public void characterizeErrorMessageFromBadRunner() {
83 assertEquals(
84 "Custom runner class BadRunner should have a public constructor with signature BadRunner(Class testClass)",
85 JUnitCore.runClasses(Empty.class).getFailures().get(0)
86 .getMessage());
87 }
88 }