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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 import java.io.File;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.concurrent.ConcurrentHashMap;
45
46 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
47 import org.apache.maven.surefire.api.booter.BaseProviderFactory;
48 import org.apache.maven.surefire.api.booter.ProviderParameterNames;
49 import org.apache.maven.surefire.api.report.ReporterConfiguration;
50 import org.apache.maven.surefire.api.report.ReporterFactory;
51 import org.apache.maven.surefire.api.suite.RunResult;
52 import org.apache.maven.surefire.api.testset.TestSetFailedException;
53 import org.apache.maven.surefire.api.util.TestsToRun;
54 import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
55 import org.apache.maven.surefire.common.junit4.Notifier;
56 import org.junit.Rule;
57 import org.junit.Test;
58 import org.junit.rules.ExpectedException;
59 import org.junit.runner.Description;
60 import org.junit.runner.RunWith;
61 import org.junit.runner.notification.RunNotifier;
62 import org.junit.runners.BlockJUnit4ClassRunner;
63 import org.junit.runners.model.InitializationError;
64
65 import static junit.framework.Assert.assertEquals;
66 import static org.apache.maven.surefire.junitcore.ConcurrentRunListener.createInstance;
67 import static org.mockito.Mockito.mock;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public class Surefire746Test {
102 @Rule
103 public final ExpectedException exception = ExpectedException.none();
104
105 @Test
106 @SuppressWarnings("checkstyle:methodname")
107 public void surefireIsConfused_ByMultipleIgnore_OnClassLevel() throws Exception {
108 ReporterFactory reporterFactory = JUnitCoreTester.defaultNoXml();
109 BaseProviderFactory providerParameters = new BaseProviderFactory(true);
110 providerParameters.setReporterFactory(reporterFactory);
111
112 providerParameters.setReporterConfiguration(new ReporterConfiguration(new File(""), false));
113 Map<String, String> junitProps = new HashMap<>();
114 junitProps.put(ProviderParameterNames.PARALLEL_PROP, "none");
115
116 JUnitCoreParameters jUnitCoreParameters = new JUnitCoreParameters(junitProps);
117
118 final Map<String, TestSet> testSetMap = new ConcurrentHashMap<>();
119
120 ConcurrentRunListener listener = createInstance(testSetMap, reporterFactory, false, false);
121
122 TestsToRun testsToRun = new TestsToRun(Collections.<Class<?>>singleton(TestClassTest.class));
123
124 org.junit.runner.notification.RunListener jUnit4RunListener = new JUnitCoreRunListener(listener, testSetMap);
125
126 List<org.junit.runner.notification.RunListener> customRunListeners = new ArrayList<>();
127 customRunListeners.add(0, jUnit4RunListener);
128
129 try {
130
131
132 exception.expect(TestSetFailedException.class);
133 JUnit4RunListener dummy = new JUnit4RunListener(new MockReporter());
134 new JUnitCoreWrapper(new Notifier(dummy, 0), jUnitCoreParameters, mock(ConsoleLogger.class))
135 .execute(testsToRun, customRunListeners, null);
136 } finally {
137 RunResult result = reporterFactory.close();
138 assertEquals("JUnit should report correctly number of test ran(Finished)", 1, result.getCompletedCount());
139 }
140 }
141
142
143
144
145 @RunWith(TestCaseRunner.class)
146 public static class TestClassTest {
147 @Test
148 public void shouldNeverBeCalled() throws Exception {}
149 }
150
151
152
153
154 public static class TestCaseRunner extends BlockJUnit4ClassRunner {
155 public TestCaseRunner(Class<?> klass) throws InitializationError {
156 super(klass);
157 }
158
159 @Override
160 public void run(RunNotifier notifier) {
161 notifier.addListener(new Listener());
162 super.run(notifier);
163 }
164 }
165
166 private static class Listener extends org.junit.runner.notification.RunListener {
167 @Override
168 public void testFinished(Description description) throws Exception {
169 throw new RuntimeException(
170 "This Exception will cause Surefire to receive an internal JUnit Description and fail.");
171 }
172 }
173 }