Search code examples
scalatestingspecs2

specs2 After method runs before the example


I have the following test:

class Foo extends mutable.SpecificationWithJUnit {
sequential

"this example should run before the 'After' method" in new Context {
    bar must beSome
}

class Context extends mutable.BeforeAfter with mutable.Around {

  override def apply[T : AsResult](a: =>T): Result = {
  lazy val result = super[Around].apply(a)
  super[BeforeAfter].apply(result)
  }

  override def delayedInit(x: => Unit): Unit = around { try { before; x; Success() } finally { after }}

  @Resource var barReader : BarReader = _

  val bar = barReader.someBar

  override def before : Any = { //some stuff}

  def after: Any = {
    bar = None
  }

  override def around[T : AsResult](t: =>T) = {
     //spring context injection logic
     AsResult.effectively(t)
  }

  }
}
}

I expect this test to pass but in reality what happens is that because of the delayed init, the after runs before the example. If I change the Context to a trait I lose the delayed init functionality. Is this a bug or am I doing something wrong?

**Edited: This example will throw an NPE when the Context is a trait. What I expect to happen is that because of the delayed-init, the Context's constructor, which consequentially means the barReader.someBar will run only after the barReader has been injected.

Thanks Netta


Solution

  • Simple answer, looks like this can't be done.