@@ -22,7 +22,8 @@ package org.scalatestplus.junit {
22
22
// -m org.scalatest.junit when running the test target for this project.
23
23
package helpers {
24
24
25
- import org .junit .runner .RunWith
25
+ import org .junit .runner .{Description , RunWith }
26
+ import org .junit .runner .manipulation .{Filter => TestFilter }
26
27
27
28
@ RunWith (classOf [JUnitRunner ])
28
29
class EasySuite extends FunSuite {
@@ -74,6 +75,34 @@ package org.scalatestplus.junit {
74
75
assert(1 === 2 )
75
76
}
76
77
}
78
+
79
+ class NameFilter (namePattern : String ) extends TestFilter {
80
+ override def shouldRun (description : Description ): Boolean = {
81
+ description.getClassName().contains(namePattern)
82
+ }
83
+
84
+ override def describe (): String = this .toString
85
+ }
86
+
87
+ //
88
+ // Suite to be filtered out by the name filter
89
+ //
90
+ @ RunWith (classOf [JUnitRunner ])
91
+ class FilteredOutSuite extends FunSuite with BeforeAndAfterAll {
92
+ test(" JUnit ran this OK!" ) {
93
+ assert(1 === 1 )
94
+ }
95
+ }
96
+
97
+ //
98
+ // Suite not to be filtered by the name filter
99
+ //
100
+ @ RunWith (classOf [JUnitRunner ])
101
+ class FilteredInSuite extends FunSuite with BeforeAndAfterAll {
102
+ test(" JUnit ran this OK!" ) {
103
+ assert(1 === 1 )
104
+ }
105
+ }
77
106
}
78
107
79
108
import org .junit .runner .Description
@@ -82,6 +111,8 @@ package org.scalatestplus.junit {
82
111
import org .junit .runner .notification .RunNotifier
83
112
import org .scalatestplus .junit .helpers .EasySuite
84
113
import org .scalatestplus .junit .helpers .KerblooeySuite
114
+ import org .scalatestplus .junit .helpers .{FilteredInSuite , FilteredOutSuite , NameFilter }
115
+ import scala .util .Try
85
116
86
117
class JUnitRunnerSuite extends FunSuite {
87
118
@@ -137,5 +168,31 @@ package org.scalatestplus.junit {
137
168
assert(result.getFailureCount === kerblooeySuite.failedCount)
138
169
assert(result.getIgnoreCount === kerblooeySuite.ignoreCount)
139
170
}
171
+
172
+ test(" Test a suite can be filtered by name" +
173
+ " as the runner implements filterable now" )
174
+ {
175
+ val runNotifier =
176
+ new RunNotifier {
177
+ var ran : List [Description ] = Nil
178
+ override def fireTestFinished (description : Description ): Unit = {
179
+ ran = description :: ran
180
+ }
181
+ }
182
+
183
+ val runners = (new JUnitRunner (classOf [FilteredOutSuite ])) ::
184
+ (new JUnitRunner (classOf [FilteredInSuite ])) :: Nil
185
+
186
+ val filter = new NameFilter (" FilteredIn" )
187
+ val filteredRunners = runners.flatMap(runner => Try {
188
+ runner.filter(filter)
189
+ runner
190
+ }.toOption.toList)
191
+
192
+ filteredRunners.foreach(_.run(runNotifier))
193
+ assert(runNotifier.ran.size === 1 )
194
+ assert(runNotifier.ran.head.getDisplayName ===
195
+ " JUnit ran this OK!(org.scalatestplus.junit.helpers.FilteredInSuite)" )
196
+ }
140
197
}
141
198
}
0 commit comments