Scalatest example using FlatSpec and ShouldMatchers


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

A simple Scala unit test example using FlatSpec and ShouldMachers in scalatest


Copy this code and paste it in your HTML
  1. package scalaunittest
  2.  
  3. import org.scalatest.FlatSpec
  4. import org.scalatest.matchers.ShouldMatchers
  5.  
  6. class ListFlatSpec extends FlatSpec with ShouldMatchers {
  7. val xs = List(1, 2, 3)
  8.  
  9. "A List" should "have length as count of elements in it" in {
  10. xs.length should be (3)
  11. }
  12.  
  13. it should "contains elements passed in the factory method" in {
  14. xs.contains(1) should be (true)
  15. xs.contains(2) should be (true)
  16. xs.contains(3) should be (true)
  17. }
  18.  
  19. it should "throw IndexOutOfBounds exception when index is out of bounds" in {
  20. evaluating {
  21. xs(4)
  22. } should produce [IndexOutOfBoundsException]
  23. }
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.