Search code examples
scalascalatest

Implement abstract method inside of scalatest class


I am new to the scalatest framework. I have a trait that has 2 abstract methods which I need to implement in the test class.

trait PropsTrait {

def set()

def break()

}

And the scalatest class looks like this

class fooTest extends WordSpec with PropsTrait with GivenWhenThen with Matchers {


  override def set(): Unit = {
    println("Url")
  }


  "The helloWorld method" when {
    "called with a specific Name " should {
      "return the respective Value as a String" in {

       val url = "test"
       url should be("test")
       }
     }
   }


  override def break(): Unit = {
    println("cleanUp")
  }
}

I don't see Url getting printed, in other words I don't see the overriden method def set() being called.

What am I missing? All that is getting executed is the WordSpec. Any help is appreciated. Thanks!


Solution

  • I don't see a call to your function set or break. What you can do is, create an object of trait inside your test class, instead of mixing it. Then you can do something like:

    val obj = new PropsTrait { override def set = {body} override def break = {body} }

    And then use this object to test the method of your trait.

    And if you want to follow your own approach, then make a call to set. Hope this will help.