Mixing into the test class

Mixing in a trait into a test class is only possible if the trait does not extend any other class explicitly, hence the super class of the trait and the test will be the same. Other than this, everything else is absolutely the same as done previously.

Let's test the A trait from earlier in this chapter, which says hello. We've also added an extra pass method, and now the trait looks as follows:

trait A {
def hello(): String = "Hello, I am trait A!"
def pass(a: Int): String = s"Trait A said: 'You passed $a.'"
}

This is what the unit test will look like:

package com.ivan.nikolov.composition

import org.scalatest.{FlatSpec, Matchers}

class TraitATest extends FlatSpec with Matchers with A {

"hello" should "greet properly." in {
hello() should equal("Hello, I am trait A!")
}

"pass" should "return the right string with the number." in {
pass(10) should equal("Trait A said: 'You passed 10.'")
}

it should "be correct also for negative values." in {
pass(-10) should equal("Trait A said: 'You passed -10.'")
}
}