Search code examples
scalajunitsbtscalatest

How come Scalatest can be used via Junit in Scala


In the build.sbt of an Example Assignment in a Scala course, the test library used is junit 4.10. There is no mention of Scalatest.

libraryDependencies += "junit" % "junit" % "4.10" % Test

And yet in a test class scalatest could be referenced and the tests could be written using actual scalatest syntax:

import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class ListsSuite extends FunSuite with Matchers {
  ...etc...
}

Question: I suppose the Scala compiler accesses scalatest via the junit library. If so, what is the reason to embed scalatest in junit?


Solution

  • I checked the assignment project from the referred course and it actually has quite complicated structure using deprecated way of defining a build in project/*.scala files, extending Build.

    The answer to your question is simple though: scalatest dependency is defined in project/CommonBuild.scala and added to the build in project/StudentBuildLike.scala. So there is no magic, you're using normal scalatest and a custom test runner (see project/ScalaTestRunner.scala).