Skip to content

Commit 6982fad

Browse files
committed
Make JUnitRunner Filterable
1 parent bf6314e commit 6982fad

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/main/scala/org/scalatestplus/junit/JUnitRunner.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.scalatest._
2020
import org.junit.runner.notification.RunNotifier
2121
import org.junit.runner.notification.Failure
2222
import org.junit.runner.Description
23+
import org.junit.runner.manipulation.{Filter => TestFilter, Filterable, NoTestsRemainException}
2324

2425
/*
2526
I think that Stopper really should be a no-op, like it is, because the user has
@@ -56,7 +57,7 @@ import org.junit.runner.Description
5657
* @author Jon-Anders Teigen
5758
* @author Colin Howe
5859
*/
59-
final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.junit.runner.Runner {
60+
final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.junit.runner.Runner with Filterable {
6061

6162
private val canInstantiate = JUnitHelper.checkForPublicNoArgConstructor(suiteClass)
6263
require(canInstantiate, "Must pass an org.scalatest.Suite with a public no-arg constructor")
@@ -113,5 +114,10 @@ final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.jun
113114
* @return the expected number of tests that will run when this suite is run
114115
*/
115116
override def testCount() = suiteToRun.expectedTestCount(Filter())
117+
118+
override def filter(filter: TestFilter): Unit = {
119+
if (!filter.shouldRun(getDescription)) throw new NoTestsRemainException
120+
}
121+
116122
}
117123

src/test/scala/org/scalatestplus/junit/JUnitRunnerSuite.scala

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 {
@@ -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 {
87118

@@ -137,5 +168,31 @@ package org.scalatestplus.junit {
137168
assert(result.getFailureCount === kerblooeySuite.failedCount)
138169
assert(result.getIgnoreCount === kerblooeySuite.ignoreCount)
139170
}
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+
}
140197
}
141198
}

0 commit comments

Comments
 (0)