Scalatest example using FunSuite, expecting exception


/ Published in: Scala
Save to your folder(s)

A simple Scala unit test example using FunSuite in scalatest. The test expects an exception.


Copy this code and paste it in your HTML
  1. package scalaunittest
  2.  
  3. import org.scalatest.FunSuite
  4. import org.scalatest.junit.JUnitRunner
  5. import org.junit.runner.RunWith
  6.  
  7. @RunWith(classOf[JUnitRunner]) // Run As | JUnit Test
  8. class ListFunSuite extends FunSuite {
  9. /*
  10. * This is a test. The test method takes two arguments.
  11. * The first argument is the test name or description
  12. * The second argument is a block of code to execute, that is, the test body.
  13. */
  14. test("list length") {
  15. val ls = List(2, 3, 4)
  16.  
  17. // This is the way to expect an exception.
  18. intercept[IndexOutOfBoundsException] {ls(5)}
  19. }
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.