34

I'am toying with Specs2 and ScalaTest for BDD in Scala. I've written expectations in Specs2 when I am asserting that a given exception should not be thrown.

"do something" in {
 {
   ....
 } must not(throwA[MyException])
}

I was hoping to be able to write the equivalent in ScalaTest like:

"do something" in {
 evaluating {
   ....
 } should not produce[MyException]
}

But this does not compile and I could not find way of doing it. Is that even possible?

Many thanks in advance.

Guillaume Belrose
  • 2,478
  • 4
  • 23
  • 24
  • Interesting. Can you elaborate on what the use case is? I would think you'd want something more specific. Either it should throw a particular exception or it should not throw any exception. What you're saying is you want to assert that the expression can either return normally or throw any exception except for the one you don't want to see. If your use case makes sense I can add the "not produce" syntax. Thanks. – Bill Venners Aug 09 '11 at 19:08
  • @Bill, having thought a bit longer about my current use case, I think the default behaviour of ScalaTest (failing the test if any exception occurs) if sufficient. Thanks. – Guillaume Belrose Aug 11 '11 at 09:19

2 Answers2

77

The current version of ScalaTest does support this:

noException should be thrownBy 0 / 1

See docs.

Wolfram Arnold
  • 7,159
  • 5
  • 44
  • 64
  • 9
    I come to this answer about once per month. One day I'll memorize it. – Def_Os Apr 29 '21 at 23:36
  • I am using the `GivenWhenThen` trait and in my `Then` clause I pasted this code. What does the 0 / 1 syntax mean? I couldn't find it in the docs. – sandbar Mar 08 '22 at 04:49
  • 1
    @sandbar: "0/1" is a division of zero divided by one. This is just an example operation that doesn't throw an exception. Compare with "1/0" which will this a division by zero exception. I could have used any other operation that doesn't throw any exception. – Wolfram Arnold Mar 09 '22 at 15:44
  • I'm actually not sure what the use case of this is. If the function or operation throws an exception the test will fail anyways. Is this just to make it clear what should be happening in the test? – sandbar Jan 10 '23 at 02:00
  • @sandbar: This is typically used when fixing bugs. The code encountered an exceptional condition, due to a bug, that it should have handled. When fixing bugs, it's best practice to first reproduce the issue in a test, see the test fail (i.e. the exception being thrown in the code under test), and then to fix the bug and see the test pass. – Wolfram Arnold Feb 22 '23 at 20:49
8

This is not possible directly in the latest version of ScalaTest because the method should of EvaluatingApplicationShouldWrapper does not have an overload that takes a NotWord, only one that takes a ResultOfProduceInvocation[T].

I'd suggest just letting the undesired exception happen, which will fail the test. This is the classic way.

But if you feel you need more clarity about what failed exactly, you could use a try-catch block to handle the error. If you catch the error you don't want to happen, handle the exception with a call to the fail method:

fail("That expression shouldn't have thrown a MyExceptionType exception")
traffichazard
  • 1,247
  • 1
  • 9
  • 13
  • 4
    As of 2016, this is no longer true. See Wolfram Arnold's answer. – francoisr Apr 13 '16 at 12:27
  • 1
    This way it's possible to only fail on specific exception. Using try-catch and fail():```try { doSomething() } catch { case ex: SomeException => fail("Should not throw SomeException", ex) }``` – Bohumir Zamecnik Jul 21 '17 at 05:01