ScalaTest 108 - How to test expected exceptions with ‘intercept’

Problem: Using ScalaTest, you want to test a portion of your code that should throw an exception under certain conditions.


Use the intercept method to verify the exception occurs. In the following example, the boom method will always throw an exception. The intercept method lets you catch that exception, so you can verify that this portion of the method works as desired:

test ("catching an exception") {
  val thrown = intercept[Exception] {
  assert(thrown.getMessage === "Boom!")

Here’s how this code works:

  • If boom throws an exception, intercept will catch and return that exception. This lets you assign it to a variable like thrown.
  • If boom completes normally, or throws an exception you weren’t expecting, intercept will complete abruptly with a TestFailedException.

As shown, you catch the exception in a value named something like thrown, and then test the message from the exception inside an assert method.


This example used intercept to catch the exception, and assert to test the exception message, but this isn’t the only possible solution.

The following code shows that you don’t have to catch the exception as an object and then test its message. Because intercept will end the test with a TestFailedException if your code doesn’t throw an exception, your test can be as simple as this:

test ("catching an exception") {
  intercept[Exception] { pizza.boom }

If your code throws the exception, intercept catches it, and the test succeeds. (You expected it to throw an exception, and it did.)

Conversely, if your method doesn’t throw an exception, intercept will make the test fail. The output looks like this:

[info] - catching an exception *** FAILED ***
[info]   Expected exception java.lang.Exception to be thrown, 
         but no exception was thrown. (Test.scala:27)

Of course, you can also use a try/catch block to test that the exception occurs under the right situations, but intercept was created as a way to assist with this common testing pattern.

See Also

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote, and was published by O’Reilly in late 2013:

You can find the Scala Cookbook at these locations:

I hope it has been helpful. All the best, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.