Mixing into the test cases

We can also mix traits into the individual test cases separately. This could allow us to apply customizations specific to those test cases only. The following is just a different representation of the preceding unit test:

package com.ivan.nikolov.composition

import org.scalatest.{FlatSpec, Matchers}

class TraitACaseScopeTest extends FlatSpec with Matchers {
"hello" should "greet properly." in new A {
hello() should equal("Hello, I am trait A!")
}

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

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

As you can see in the preceding code, the test cases are identical to the previous ones. They, however, individually mix A in. This would allow us to apply different customizations in the cases where a trait requires an implementation of a method or a variable initialization. This way, we can also focus specifically on the trait being tested, rather than creating actual instances of it.