Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/scala/org/scalatestplus/junit/JUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.scalatest._
import org.junit.runner.notification.RunNotifier
import org.junit.runner.notification.Failure
import org.junit.runner.Description
import org.junit.runner.manipulation.{Filter => TestFilter, Filterable, NoTestsRemainException}

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

private val canInstantiate = JUnitHelper.checkForPublicNoArgConstructor(suiteClass)
require(canInstantiate, "Must pass an org.scalatest.Suite with a public no-arg constructor")
Expand Down Expand Up @@ -113,5 +114,10 @@ final class JUnitRunner(suiteClass: java.lang.Class[_ <: Suite]) extends org.jun
* @return the expected number of tests that will run when this suite is run
*/
override def testCount() = suiteToRun.expectedTestCount(Filter())

override def filter(filter: TestFilter): Unit = {
if (!filter.shouldRun(getDescription)) throw new NoTestsRemainException
}

}

59 changes: 58 additions & 1 deletion src/test/scala/org/scalatestplus/junit/JUnitRunnerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package org.scalatestplus.junit {
// -m org.scalatest.junit when running the test target for this project.
package helpers {

import org.junit.runner.RunWith
import org.junit.runner.{Description, RunWith}
import org.junit.runner.manipulation.{Filter => TestFilter}

@RunWith(classOf[JUnitRunner])
class EasySuite extends FunSuite {
Expand Down Expand Up @@ -74,6 +75,34 @@ package org.scalatestplus.junit {
assert(1 === 2)
}
}

class NameFilter(namePattern: String) extends TestFilter {
override def shouldRun(description: Description): Boolean = {
description.getClassName().contains(namePattern)
}

override def describe(): String = this.toString
}

//
// Suite to be filtered out by the name filter
//
@RunWith(classOf[JUnitRunner])
class FilteredOutSuite extends FunSuite with BeforeAndAfterAll {
test("JUnit ran this OK!") {
assert(1 === 1)
}
}

//
// Suite not to be filtered by the name filter
//
@RunWith(classOf[JUnitRunner])
class FilteredInSuite extends FunSuite with BeforeAndAfterAll {
test("JUnit ran this OK!") {
assert(1 === 1)
}
}
}

import org.junit.runner.Description
Expand All @@ -82,6 +111,8 @@ package org.scalatestplus.junit {
import org.junit.runner.notification.RunNotifier
import org.scalatestplus.junit.helpers.EasySuite
import org.scalatestplus.junit.helpers.KerblooeySuite
import org.scalatestplus.junit.helpers.{FilteredInSuite, FilteredOutSuite, NameFilter}
import scala.util.Try

class JUnitRunnerSuite extends FunSuite {

Expand Down Expand Up @@ -137,5 +168,31 @@ package org.scalatestplus.junit {
assert(result.getFailureCount === kerblooeySuite.failedCount)
assert(result.getIgnoreCount === kerblooeySuite.ignoreCount)
}

test("Test a suite can be filtered by name" +
"as the runner implements filterable now")
{
val runNotifier =
new RunNotifier {
var ran: List[Description] = Nil
override def fireTestFinished(description: Description): Unit = {
ran = description :: ran
}
}

val runners = (new JUnitRunner(classOf[FilteredOutSuite])) ::
(new JUnitRunner(classOf[FilteredInSuite])) :: Nil

val filter = new NameFilter("FilteredIn")
val filteredRunners = runners.flatMap(runner => Try{
runner.filter(filter)
runner
}.toOption.toList)

filteredRunners.foreach(_.run(runNotifier))
assert(runNotifier.ran.size === 1)
assert(runNotifier.ran.head.getDisplayName ===
"JUnit ran this OK!(org.scalatestplus.junit.helpers.FilteredInSuite)")
}
}
}