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