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