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.compositionimport org.scalatest.{FlatSpec, Matchers}class TraitATest extends FlatSpec with Matchers with A {  "hello" should "greet properly." ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.