Scala - unit test setup (use access modifier qualifiers?)

I'm new to Scala (from Java) and I like to develop TDD/BDD. So before knowing much about Scala I've already dived into Scalatest.

I'm wondering what you think is a good access strategy for unit testing, from the viewpoint that you want to test as much as possible.

Suppose I have a class World that I want to test and a class Agent that should not know everything about the World.

package program {
  package world {
    class World {
      private var notForAgent
      def forAgentDuringActing
      // forAgentDuringActing has an important side effect in notForAgent

  package agent {
    class Agent
    // would call World.forAgentDuringActing

  package world.test {
    class WorldSpec extends FunSpec {
      describe("The world") {
        it("should have side-effect behaviour when the agent acts on it") {
          // ... the test ...

Note that these package declarations are not sacred to me. What I really would like, is that WorldSpec would be something like a companion object to World, so that it can test for the side effect.

I thought maybe access modifier qualifiers would help. I could say private[world] notForAgent, but that really is more access than I would like. What I actually would like is something like private[this, test.WorldSpec] notForAgent, but I don't think multiple qualifiers are allowed.

What would you do to make this testable? Alternatively, can you indicate where my thoughts go in the wrong direction?

p.s. I know the statement "never test privates". But I also know the opinion "Testing is more important than the code itself. Alter access modifiers if it's needed for testing". However, I would like to avoid that discussion in this thread.


If you're asking "How to test private methods?", then ScalaTest actually supports this. See here.

class C {
  private def m(x: Int) = x

class CTests extends /*any test suite you like*/ with PrivateMethodTester {
  val decoratedM = PrivateMethod[Int]('m)
  val c = new C
  val actual = c invokePrivate decoratedM(4711)
  val expected = 4711
  actual should be(expected) // this line depends on the test suite you've chosen

Need Your Help

What's the meaning of this error message "insertId: Error: INVALID_ACCESS_ERR: DOM Exception 15"?

jquery cordova mobile-safari

I'm developing an app using jquery mobile, phonegap and openDatabase. When the app is executed, I can see in the console of Safari browser the SQLResultSet with this message:

.submit() doesn't work with Firefox and Greasemonkey

javascript greasemonkey

I'm trying to make an auto login script and I'm stuck on the submit part...