Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match a thrown Exception's message?

Tags:

scala

specs2

The specs2 Matchers guide states:

throwA[ExceptionType](message = "boom") additionally checks if the exception message is as expected

But when I use this, the message is apparently matched on the entire stacktrace instead of only the exception message.

Test

"cont'd what if -- running test again shows that the app has already died" in {
  running(new FakeApplication(additionalConfiguration = inLocalPostgresDatabase())) {
    db2 withSession {
      val comboboxOpsClass = new ComboboxOps(database)
    }
  } must throwA[SQLException](message = "Attempting to obtain a connection from a pool that has already been shutdown")
}

Stacktrace

[error]      'Attempting to obtain a connection from a pool that has already been shutdown. 
[error]      Stack trace of location where pool was shutdown follows:
[error]       java.lang.Thread.getStackTrace(Thread.java:1568)
[error]       com.jolbox.bonecp.BoneCP.captureStackTrace(BoneCP.java:572)
[error]       com.jolbox.bonecp.BoneCP.shutdown(BoneCP.java:161)

many many more lines

[error]       org.specs2.execute.ResultExecution$class.execute(ResultExecution.scala:22)
[error]       org.specs2.execute.ResultExecution$.execute(ResultExecution.scala:116)
[error]       org.specs2.specification.FragmentExecution$class.executeBody(FragmentExecution.scala:28)
[error]       
[error]       sbt.ForkMain$Run.runTestSafe(ForkMain.java:211)
[error]       sbt.ForkMain$Run.runTests(ForkMain.java:187)
[error]       sbt.ForkMain$Run.run(ForkMain.java:251)
[error]       sbt.ForkMain.main(ForkMain.java:97)
[error]      ' doesn't match '.*Attempting to obtain a connection from a pool that has already been shutdown.*' (FakeApplicationSpec.scala:138)

Could somebody point me to a working example of this usage of specs2?

like image 836
Meredith Avatar asked Jan 09 '14 20:01

Meredith


People also ask

What happens when an exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

How do you throw an exception in a message?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

How do you handle exceptions in Scala?

IOException object Demo { def main(args: Array[String]) { try { val f = new FileReader("input. txt") } catch { case ex: FileNotFoundException => { println("Missing file exception") } case ex: IOException => { println("IO Exception") } } finally { println("Exiting finally...") } } } Save the above program in Demo.


2 Answers

I hate to say it but I never found a generic way of discovering what the text was other than to either let it happen and add it after the fact... or go to the source of the exception and copy it from there. Here's what I did in Novus-JDBC, line 91:

"handle when take more than it can give" in{
  val iter0 = nonCounter()

  val iter = iter0 slice (0,10)

  (iter next () must be greaterThan 0) and
  (iter next () must be greaterThan 0) and
  (iter next () must be equalTo -1) and
  (iter next () must be equalTo 3) and
    (iter.hasNext must beFalse) and
    (iter next() must throwA(new NoSuchElementException("next on empty iterator")))
}
like image 131
wheaties Avatar answered Oct 10 '22 13:10

wheaties


You'd have the option of matching against an exception message if you implement the exception as a case class and include that message in its first constructor parameter list.

case class ObjectionException(statement: String)
extends    Exception(statement)

try throw ObjectionException("I object")
catch {
  case ObjectionException("I object")   => println("I objected to myself?")
  case ObjectionException("You object") => println("I don't care!")
  case ObjectionException(objection)    => println(s"Objection: $objection")
}

// Exiting paste mode, now interpreting.

I objected to myself?
defined class ObjectionException

scala>

You could also use a Regex to match the message:

val ContentObjection = ".*Content.*".r

try throw ObjectionException("ObjectionableContent")
catch {
  case ObjectionException(ContentObjection()) => println("Questionable Content")
  case ObjectionException(objection)          => println(s"Objection: $objection")
}

// Exiting paste mode, now interpreting.

Questionable Content
ContentObjection: scala.util.matching.Regex = .*Content.*
like image 26
Randall Schulz Avatar answered Oct 10 '22 14:10

Randall Schulz