Search code examples
scalascalatest

Unable to match exception instance in test case


The following piece of code returns an instance of an exception if a configuration is missing

if (host == "" || redirectUrl == "" || successUrlParameter == "" || failUrlParameter == "") {
      //checktest-USerTransactionService-finishResetCredentials-return exception if host configuration parameters are present
      (Some(MissingConfigurationException()),errorRediectedUrl,None)
    } else {...}

I am testing this and am matching it like the following

errorThrown mustBe Some(MissingConfigurationException())

The test case fails even though the values seem to be equal.

Expected :Some(utilities.MissingConfigurationException: Server Error. Missing Configuration)
Actual   :Some(utilities.MissingConfigurationException: Server Error. Missing Configuration)

how should I compare the expected vs actual value?


Solution

  • Exceptions are compared by reference not by value. Thus, two identical values will always be different, unless they are the same instance.
    So, you have to check for the class of the instance.

    However. Scalatest provides a better syntax for checking for class and using options.

    errorThrown.value shouldBe a [MissingConfigurationException]
    

    Reference: