1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.junitcore;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import junit.framework.TestCase;
26 import org.junit.Assume;
27 import org.junit.Test;
28 import org.junit.runner.Computer;
29 import org.junit.runner.Description;
30 import org.junit.runner.JUnitCore;
31 import org.junit.runner.Result;
32 import org.junit.runner.notification.RunListener;
33
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.hamcrest.Matchers.contains;
36 import static org.hamcrest.Matchers.containsInAnyOrder;
37 import static org.hamcrest.Matchers.empty;
38 import static org.hamcrest.Matchers.hasSize;
39
40
41
42
43 public class JUnitCoreRunListenerTest extends TestCase {
44 public void testTestRunStarted() {
45 RunListener jUnit4TestSetReporter =
46 new JUnitCoreRunListener(new MockReporter(), new HashMap<String, TestSet>());
47 JUnitCore core = new JUnitCore();
48 core.addListener(jUnit4TestSetReporter);
49 Result result = core.run(new Computer(), STest1.class, STest2.class);
50 core.removeListener(jUnit4TestSetReporter);
51 assertEquals(2, result.getRunCount());
52 }
53
54 public void testFailedAssumption() {
55 RunListener jUnit4TestSetReporter =
56 new JUnitCoreRunListener(new MockReporter(), new HashMap<String, TestSet>());
57 JUnitCore core = new JUnitCore();
58 core.addListener(jUnit4TestSetReporter);
59 Result result = core.run(new Computer(), TestWithAssumptionFailure.class);
60 core.removeListener(jUnit4TestSetReporter);
61 assertEquals(1, result.getRunCount());
62 }
63
64 public void testStateForClassesWithNoChildren() throws Exception {
65 Description testDescription = Description.createSuiteDescription("testMethod(cannot.be.loaded.by.junit.Test)");
66 Description st1 = Description.createSuiteDescription(STest1.class);
67
68 testDescription.addChild(st1);
69 Description st2 = Description.createSuiteDescription(STest2.class);
70
71 testDescription.addChild(st2);
72
73 Map<String, TestSet> classMethodCounts = new HashMap<>();
74 JUnitCoreRunListener listener = new JUnitCoreRunListener(new MockReporter(), classMethodCounts);
75 listener.testRunStarted(testDescription);
76 assertEquals(2, classMethodCounts.size());
77 Iterator<TestSet> iterator = classMethodCounts.values().iterator();
78 assertFalse(iterator.next().equals(iterator.next()));
79 }
80
81 public void testTestClassNotLoadableFromJUnitClassLoader() throws Exception {
82
83 Description testDescription = Description.createSuiteDescription("testMethod(cannot.be.loaded.by.junit.Test)");
84 assertEquals("testMethod", testDescription.getMethodName());
85 assertEquals("cannot.be.loaded.by.junit.Test", testDescription.getClassName());
86
87 assertNull(testDescription.getTestClass());
88 Description suiteDescription = Description.createSuiteDescription("testSuite");
89 suiteDescription.addChild(testDescription);
90 Map<String, TestSet> classMethodCounts = new HashMap<>();
91 JUnitCoreRunListener listener = new JUnitCoreRunListener(new MockReporter(), classMethodCounts);
92 listener.testRunStarted(suiteDescription);
93 assertEquals(1, classMethodCounts.size());
94 TestSet testSet = classMethodCounts.get("cannot.be.loaded.by.junit.Test");
95 assertNotNull(testSet);
96 }
97
98 public void testNonEmptyTestRunStarted() throws Exception {
99 Description aggregator = Description.createSuiteDescription("null");
100 Description suite = Description.createSuiteDescription("some.junit.Test");
101 suite.addChild(Description.createSuiteDescription("testMethodA(some.junit.Test)"));
102 suite.addChild(Description.createSuiteDescription("testMethodB(some.junit.Test)"));
103 suite.addChild(Description.createSuiteDescription("testMethod(another.junit.Test)"));
104 aggregator.addChild(suite);
105 Map<String, TestSet> classMethodCounts = new HashMap<>();
106 JUnitCoreRunListener listener = new JUnitCoreRunListener(new MockReporter(), classMethodCounts);
107 listener.testRunStarted(aggregator);
108 assertThat(classMethodCounts.keySet(), hasSize(2));
109 assertThat(classMethodCounts.keySet(), containsInAnyOrder("some.junit.Test", "another.junit.Test"));
110 TestSet testSet = classMethodCounts.get("some.junit.Test");
111 MockReporter reporter = new MockReporter();
112 testSet.replay(reporter);
113 assertTrue(reporter.containsNotification(MockReporter.SET_STARTED));
114 assertTrue(reporter.containsNotification(MockReporter.SET_COMPLETED));
115 listener.testRunFinished(null);
116 assertThat(classMethodCounts.keySet(), empty());
117 }
118
119 public void testEmptySuiteTestRunStarted() throws Exception {
120 Description aggregator = Description.createSuiteDescription("null");
121 Description suite = Description.createSuiteDescription("some.junit.TestSuite");
122 aggregator.addChild(suite);
123 Map<String, TestSet> classMethodCounts = new HashMap<>();
124 JUnitCoreRunListener listener = new JUnitCoreRunListener(new MockReporter(), classMethodCounts);
125 listener.testRunStarted(aggregator);
126 assertThat(classMethodCounts.keySet(), hasSize(1));
127 assertThat(classMethodCounts.keySet(), contains("some.junit.TestSuite"));
128 listener.testRunFinished(null);
129 assertThat(classMethodCounts.keySet(), empty());
130 }
131
132
133
134
135 public static class STest1 {
136 @Test
137 public void testSomething() {}
138 }
139
140
141
142
143 public static class STest2 {
144 @Test
145 public void testSomething2() {}
146 }
147
148
149
150
151 public static class TestWithAssumptionFailure {
152 @Test
153 public void testSomething2() {
154 Assume.assumeTrue(false);
155 }
156 }
157 }