Search code examples
scalaspecs2

Before and After in the same trait


I have next code test in scala, and i want before and after methods run before and after each tests, but i have an error

This is my test

import java.util.{Calendar, UUID}
import com.smartchat.helpers.BeforeAllAfterAll
import com.smartchat.models.SmartChat.{ComplaintReasonType,
ComplaintContentType}
import org.junit.runner.RunWith
import org.specs2.execute.{Result, AsResult}
import org.specs2.mutable.{After, Before}
import org.specs2.runner.JUnitRunner
import scala.concurrent.duration._
import scala.concurrent.{Future, Await}


  class ComplaintModelTest extends DatabaseSpec{

  class TestingEnvironment extends ComplaintComponentImpl 
  with StorageComponentImpl

  val environment = new TestingEnvironment

  "Complaint model" should {


    "test get complaints by reason" in new BeforeAfterEach {
      ok
    }

    "test get complaints by type" in new BeforeAfterEach {
      ok
    }
  }

}

trait BeforeAfterEach extends Before with After{

  override def before = {
    println("++++++++++++++")
  }

  override def after = {
    println("--------------")
  }

}

this is output of the error

trait BeforeAfterEach inherits conflicting members:
[error]   method apply in trait Before of type [T](a: => T)(implicit evidence$3: org.specs2.execute.AsResult[T])org.specs2.execute.Result  and
[error]   method apply in trait After of type [T](a: => T)(implicit evidence$1: org.specs2.execute.AsResult[T])org.specs2.execute.Result
[error] (Note: this can be resolved by declaring an override in trait BeforeAfterEach.)
[error] trait BeforeAfterEach extends Before with After{
[error]       ^
[error] one error found
[error] (test:compileIncremental) Compilation failed

I dont know how to fix it. I want methods run before and after each test example


Solution

  • If you want to define before and after methods in BeforeAfterEach, then you should inherit from org.specs2.mutable.BeforeAfter.