Advertisement
mitrakov

Simple FlatSpec

Feb 19th, 2019 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.65 KB | None | 0 0
  1. // build.sbt:
  2. libraryDependencies ++= Seq(
  3.   "org.scalatest"     %% "scalatest"            % "3.0.5"         % Test,
  4.   "org.mockito"       %% "mockito-scala"        % "1.0.9"         % Test
  5. )
  6.  
  7. // ClassA.scala
  8. class ClassA(dependentClass: DependentClass) {
  9.   private lazy val task = Task(0)
  10.   private lazy val user = User(None, "", "", None)
  11.  
  12.   def getCurrentTaskDuration: Long = {
  13.     dependentClass.getDurationMsec(task, user)
  14.   }
  15.  
  16.   def sayHello: String = "hello"
  17. }
  18.  
  19. // DependentClass.scala:
  20. class DependentClass {
  21.   def getDurationMsec(task: Task, user: User): Long = ???
  22. }
  23.  
  24.  
  25.  
  26. // Spec:
  27. import org.scalatest.FlatSpec
  28. import org.scalatest._
  29. import org.scalatest.mockito.MockitoSugar
  30. import org.mockito.Mockito._
  31. import org.mockito.ArgumentMatchers._
  32.  
  33. import my.package._
  34.  
  35. class MySpec extends FlatSpec with Matchers with MockitoSugar {
  36.   behavior of "ClassA"
  37.  
  38.   val dependentClass: DependentClass = mock[DependentClass]
  39.   val classA = new ClassA(dependentClass)
  40.  
  41.   when(dependentClass.getDurationMsec(any[Task], any[User])).thenReturn(1000)
  42.  
  43.   "A classA" should "return 1000 for a given task" in {
  44.     val result = classA.getCurrentTaskDuration
  45.     result shouldBe 1000
  46.   }
  47.  
  48.   it should "say hello" in {
  49.     val result = classA.sayHello
  50.     result shouldBe "hello"
  51.   }
  52. }
  53.  
  54.  
  55.  
  56. // Output:
  57. [info] MySpec:
  58. [info] A classA
  59. [info] - should return 1000 for a given task
  60. [info] - should say hello
  61. [info] ScalaTest
  62. [info] Run completed in 2 seconds, 214 milliseconds.
  63. [info] Total number of tests run: 2
  64. [info] Suites: completed 1, aborted 0
  65. [info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
  66. [info] All tests passed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement