score:3

Accepted answer

i am assuming dosomething() performs some operation that is dependent on application state.

try this:

class applicationspec extends freespec with beforeandafterall with oneserverpersuite{

  override protected def beforeall(): unit = {
    dosomething()
  }

  "application test" - {
    "first test" in {
      ...
    }
  }

}

the problem is you are possibly mixin linearization in wrong order. by mixin oneserpersuite before beforeandafterall, the order in which the super.run() is called is reversed, leading to beforeall() being called before application start.

from the git repo of the two projects:

 //beforeandafterall
 abstract override def run(testname: option[string], args: args): status = {
    if (!args.runtestinnewinstance && (expectedtestcount(args.filter) > 0 || invokebeforeallandafterallevenifnotestsareexpected))
      beforeall()

    val (runstatus, thrownexception) =
      try {
        (super.run(testname, args), none)
      }
      catch {
        case e: exception => (failedstatus, some(e))
      }
    ...
   }


    //oneserverpersuite
    abstract override def run(testname: option[string], args: args): status = {
    val testserver = testserver(port, app)
    testserver.start()
    try {
      val newconfigmap = args.configmap + ("org.scalatestplus.play.app" -> app) + ("org.scalatestplus.play.port" -> port)
      val newargs = args.copy(configmap = newconfigmap)
      val status = super.run(testname, newargs)
      status.whencompleted { _ => testserver.stop() }
      status
    } catch { // in case the suite aborts, ensure the server is stopped
      case ex: throwable =>
        testserver.stop()
        throw ex
    }
  }

so by putting the oneserverpersuite trait in the end, it will first initialize the application, then call super.run() which will call the run method inside beforeandafterall which will execute beforeall() and then invoke super.run() of freespec which will execute the tests.


Related Query

More Query from same tag