Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScalaTest thrownBy testing exception message

To test if my code is throwing expected exception I use the following syntax:

  an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))

is there any way to test if thrown exception contains expected message like:

  an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile 

?

like image 329
Łukasz Rzeszotarski Avatar asked Jul 04 '15 00:07

Łukasz Rzeszotarski


People also ask

How do I ignore ScalaTest?

When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag. Thus, marking the SetSpec in the above example with the @Ignore tag annotation means that both tests in the class will be ignored.

How do you catch exceptions in Scala?

The try/catch construct is different in Scala than in Java, try/catch in Scala is an expression. The exception in Scala and that results in a value can be pattern matched in the catch block instead of providing a separate catch clause for each different exception.


1 Answers

Documentation:

the [ArithmeticException] thrownBy 1 / 0 should have message "/ by zero"

and using that example:

 the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage
like image 96
bjfletcher Avatar answered Oct 02 '22 20:10

bjfletcher