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