0

I have some code running inside Future.respond, that throws an NPE, because of a bug. The problem is that my unit tests totally missed it, and are all happily passing, because the NPE is swallowed by the RootMonitor.

So, my question is if there is any way to override the RootMonitor behavior for the unit tests to result in a test failure rather than swallowing the exception?

I know, I could just catch the exception inside respond, but that's kinda backwards - if I thought there could be an exception, I would've fixed it in the first place. That's exactly a kind of situation I would like my test to catch.

So, what I am looking for is a way to override the RootMonitor globally for the tests, or else to assert somehow that it handled no exceptions after the test finishes. Is there a way to do something like this? How do people usually deal with this kind of tests?

Dima
  • 39,570
  • 6
  • 44
  • 70

1 Answers1

1

Ok, I think, I found a solution. It seems a bit kludgy, so if someone can come up with a better way, please chime in, but here goes.

   class MonitoredSuite extends FunSuite {
     protected override def withFixture(test: NoArgTest): Outcome = {
       var exception: Throwable = null
       Monitor.using(Monitor.mk {
         case e =>
         exception = e
         false
       })(test())  match {
         case x@Exceptional(_) => x
         case s if exception == null => s
         case _ => Exceptional(exception)
      }
    }         
   }

Basically, I install a noop monitor before each test, and then generate a failure if it was invoked.

Dima
  • 39,570
  • 6
  • 44
  • 70