Advertisement
mitrakov

Simple Unit Test

Oct 16th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.20 KB | None | 0 0
  1. // Simple Unit Test w/o Futures
  2. // For testing Future[+T], please see https://pastebin.com/eHqiDQWf
  3.  
  4. // SimpleService.scala:
  5. class SimpleService {
  6.   def getAverage(lst: Traversable[Int]): Int = {
  7.     lst.sum / lst.size
  8.   }
  9. }
  10.  
  11. // SimpleServiceSpec.scala:
  12. import org.scalatest.mockito.MockitoSugar
  13. import org.scalatestplus.play.PlaySpec
  14. import services.SimpleService
  15.  
  16. class SimpleServiceSpec extends PlaySpec with MockitoSugar {
  17.   val service = new SimpleService
  18.  
  19.   "A SimpleService" when {
  20.     "average is processed for 123456" should {
  21.       "return 3" in {
  22.         service.getAverage(List(1, 2, 3, 4, 5, 6)) mustBe 3
  23.       }
  24.     }
  25.     "average is processed for empty list" should {
  26.       "throw Exception" in {
  27.         an [ArithmeticException] mustBe thrownBy (service.getAverage(Nil))
  28.       }
  29.     }
  30.   }
  31. }
  32.  
  33. // Output:
  34. SimpleServiceSpec:
  35. A SimpleService
  36.   when average is processed for 123456
  37.   - should return 3.5
  38.   when average is processed for empty list
  39.   - should throw Exception
  40. ScalaTest
  41. Run completed in 1 second, 144 milliseconds.
  42. Total number of tests run: 2
  43. Suites: completed 1, aborted 0
  44. Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
  45. All tests passed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement