I am trying to write a unit test for scala object with using scalatest but.
I imported the following dependencies for my sbt
;
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
How to write the test case for this code?
object Word {
def readFile(): Map[String, Int] = {
val counter = scala.io.Source.fromFile("filepath")
.getLines.flatMap(_.split("\\W+"))
.foldLeft(Map.empty[String, Int]) {
(count, word) =>
count + (word ->
(count.getOrElse(word, 0) + 1))
}
counter
}
}
There are many ways to write test for it. There are libraries like FlatSpec, FunSuit. So here, i am going to write test case for it using FunSuit
import org.scalatest.FunSuite
class WordTest extends FunSuite {
test("should return word count ") {
val wordCount = Word.readFile()
assert(wordCount.nonEmpty)
}
}