Exception thrown in beforeAll not handled

115 views
Skip to first unread message

ben.to...@gmail.com

unread,
Jun 24, 2015, 1:40:09 AM6/24/15
to specs2...@googlegroups.com
I have noticed that the runner does not stop when an exception is thrown by beforeAll, rather the spec is left in a potentially inconsistent state.  Here is an example:

import org.specs2.mutable.Specification
import org.specs2.specification.BeforeAfterAll

class TestSpec extends Specification with BeforeAfterAll {

  var value = 0

  override def beforeAll(): Unit ={
    if (1 == 1)
      throw new Exception()
    value = 1
  }

  override def afterAll(): Unit = {}

  "value" >> {
    "equals one" >> {
      value must_== 1
    }
  }

}

After executing, I do not see the exception reported, but rather the following:


Testing started at 10:32 PM ...

'0' is not equal to '1'
Expected :1
Actual   :0
 <Click to see difference>

java.lang.Exception: '0' is not equal to '1'



Is this by design?  I would have expected an exception in beforeAll to fail the entire spec.

I see this in both IntelliJ and SBT.

etorreborre

unread,
Jun 24, 2015, 3:40:17 AM6/24/15
to specs2...@googlegroups.com, ben.to...@gmail.com
This is "by design" in the sense that the "beforeAll" method creates a Step in your specification and a Step only influence the sequencing of the spec.

You can fix this by using the following trait instead:

trait BeforeAfterAllStopOnError extends SpecificationStructure with FragmentsFactory {
def beforeAll
def afterAll
override def map(fs: =>core.Fragments) = super.map(fs).prepend(fragmentFactory.step(beforeAll).stopOnError).append(fragmentFactory.step(afterAll))
}

In that trait I specify that the beforeAll step should stop if there is an error. 
I am not sure that this is what everyone wants to have. For example if your setup method fails you might still want afterAll to run in order to clean up.

Eric.

ben.to...@gmail.com

unread,
Jun 24, 2015, 4:45:04 PM6/24/15
to specs2...@googlegroups.com, ben.to...@gmail.com
Thanks for the reply.  When I use this code example and import the types, the compiler complains that "core" cannot be found.  I am using specs2 3.6.1.

Any tips on where to look?

Ben

etorreborre

unread,
Jun 24, 2015, 4:49:18 PM6/24/15
to specs2...@googlegroups.com, ben.to...@gmail.com
You need to import

 import org.specs2.specification.core

E.
Reply all
Reply to author
Forward
0 new messages