John Hungerford
John Hungerford

Reputation: 125

Why does it matter what order I mix in TestSuiteMixIn traits in scalatest suites?

I created the following fixtures:

trait DatabaseFixture extends TestSuiteMixin { this: TestSuite =>

    // Just setting up a test database
    val cpds = new ComboPooledDataSource
    val url : URL = getClass.getResource( "c3p0.properties" )
    val db = Database.forDataSource(cpds, Some(50))
    val users = Schema.users
    val instances = Schema.instances

    Await.result(db.run( DBIO.seq(
        users.schema.create,
    ) ), 3 seconds )

    abstract override def withFixture(test: NoArgTest): Outcome = {
        try super.withFixture(test)
        finally cpds.close()
    }
}

trait UserControllerFixture extends DatabaseFixture with ScalatraSuite { this: TestSuite =>
    addServlet( new UserController(db), "/user/*" )

    abstract override def withFixture(test: NoArgTest): Outcome = {
        super.withFixture( test )
    }
}

Here is the first way I mixed them in to a test suite:

class UserControllerTestSuite extends DatabaseFixture with ScalatraSuite with FlatSpecLike with Matchers {

    "POST to /user/add" should "return 201 for created" in {

        post( "/instance/add" ) {
            status shouldBe  201
        }
    }

}

This failed to compile with the following error: method withFixture in trait TestSuite of type (test: UserControllerTestSuite.this.NoArgTest)org.scalatest.Outcome has weaker access privileges; it should be public

However, when I mixed the fixtures in after the other scalatest traits, it compiled fine:

class UserControllerTestSuite extends ScalatraSuite with FlatSpecLike with Matchers with DatabaseFixture {

    "POST to /user/add" should "return 201 for created" in {

        post( "/instance/add" ) {
            status shouldBe  201
        }
    }

}

What's going on here? What does it mean that withFixture() has "weaker access privileges"?

Upvotes: 0

Views: 248

Answers (1)

amer
amer

Reputation: 1682

Mixins in Scala are scanned right to left. This is why DatabaseFixture is called before other traits in case where your code works.

So before when there was some other trait (TestSuite) before DatabaseFixture with withFixture method , it tried to override it "weaker access privilege", which means exactly what it says. You cannot override public method with private for example. It has to be the same priority or higher (public > protected in your case.)

Upvotes: 2

Related Questions