Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple Unit Test w/o Futures
- // For testing Future[+T], please see https://pastebin.com/eHqiDQWf
- // SimpleService.scala:
- class SimpleService {
- def getAverage(lst: Traversable[Int]): Int = {
- lst.sum / lst.size
- }
- }
- // SimpleServiceSpec.scala:
- import org.scalatest.mockito.MockitoSugar
- import org.scalatestplus.play.PlaySpec
- import services.SimpleService
- class SimpleServiceSpec extends PlaySpec with MockitoSugar {
- val service = new SimpleService
- "A SimpleService" when {
- "average is processed for 123456" should {
- "return 3" in {
- service.getAverage(List(1, 2, 3, 4, 5, 6)) mustBe 3
- }
- }
- "average is processed for empty list" should {
- "throw Exception" in {
- an [ArithmeticException] mustBe thrownBy (service.getAverage(Nil))
- }
- }
- }
- }
- // Output:
- SimpleServiceSpec:
- A SimpleService
- when average is processed for 123456
- - should return 3.5
- when average is processed for empty list
- - should throw Exception
- ScalaTest
- Run completed in 1 second, 144 milliseconds.
- Total number of tests run: 2
- Suites: completed 1, aborted 0
- Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
- All tests passed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement