@@ -22,7 +22,8 @@ package org.scalatestplus.junit {
2222 // -m org.scalatest.junit when running the test target for this project.
2323 package helpers {
2424
25- import org .junit .runner .RunWith
25+ import org .junit .runner .{Description , RunWith }
26+ import org .junit .runner .manipulation .{Filter => TestFilter }
2627
2728 @ RunWith (classOf [JUnitRunner ])
2829 class EasySuite extends funsuite.AnyFunSuite {
@@ -74,6 +75,34 @@ package org.scalatestplus.junit {
7475 assert(1 === 2 )
7576 }
7677 }
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+ }
77106 }
78107
79108 import org .junit .runner .Description
@@ -82,6 +111,8 @@ package org.scalatestplus.junit {
82111 import org .junit .runner .notification .RunNotifier
83112 import org .scalatestplus .junit .helpers .EasySuite
84113 import org .scalatestplus .junit .helpers .KerblooeySuite
114+ import org .scalatestplus .junit .helpers .{FilteredInSuite , FilteredOutSuite , NameFilter }
115+ import scala .util .Try
85116
86117 class JUnitRunnerSuite extends funsuite.AnyFunSuite {
87118
@@ -135,5 +166,31 @@ package org.scalatestplus.junit {
135166 assert(result.getFailureCount === kerblooeySuite.failedCount)
136167 assert(result.getIgnoreCount === kerblooeySuite.ignoreCount)
137168 }
169+
170+ test(" Test a suite can be filtered by name" +
171+ " as the runner implements filterable now" )
172+ {
173+ val runNotifier =
174+ new RunNotifier {
175+ var ran : List [Description ] = Nil
176+ override def fireTestFinished (description : Description ): Unit = {
177+ ran = description :: ran
178+ }
179+ }
180+
181+ val runners = (new JUnitRunner (classOf [FilteredOutSuite ])) ::
182+ (new JUnitRunner (classOf [FilteredInSuite ])) :: Nil
183+
184+ val filter = new NameFilter (" FilteredIn" )
185+ val filteredRunners = runners.flatMap(runner => Try {
186+ runner.filter(filter)
187+ runner
188+ }.toOption.toList)
189+
190+ filteredRunners.foreach(_.run(runNotifier))
191+ assert(runNotifier.ran.size === 1 )
192+ assert(runNotifier.ran.head.getDisplayName ===
193+ " JUnit ran this OK!(org.scalatestplus.junit.helpers.FilteredInSuite)" )
194+ }
138195 }
139196}
0 commit comments