Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for additional properties of expected Exceptions using ScalaTest

I'm using ScalaTest for testing some Scala code. I currently testing for expected exceptions with code like this

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

class ImageComparisonTest extends FeatureSpec with ShouldMatchers{

    feature("A test can throw an exception") {

        scenario("when an exception is throw this is expected"){
            evaluating { throw new Exception("message") } should produce [Exception]
        }
    }
}

But I would like to add additional check on the exception, e.g. I would like to check that the exceptions message contains a certain String.

Is there a 'clean' way to do this? Or do I have to use a try catch block?

like image 687
Jens Schauder Avatar asked Nov 21 '10 11:11

Jens Schauder


1 Answers

I found a solution

val exception = intercept[SomeException]{ ... code that throws SomeException ... }
// you can add more assertions based on exception here
like image 119
Jens Schauder Avatar answered Oct 21 '22 19:10

Jens Schauder