I have a package object defined in both the main and the test code tree as shown below. in run scope the package object in main/ takes effect and in test scope the package object under test/ takes effect. For eg
src/main/scala/com/example/testbed/package.scala
package object testbed {
val foo = "bar"
}
src/test/scala/com/example/testbed/package.scala
package object testbed {
val foo = "baz"
}
src/main/scala/com/example/testbed/Main.scala
object Main extends App {
println(Run.run)
}
object Run {
def run = foo
}
src/test/scala/com/example/testbed/TestSpec.scala
class TestSpec extends FlatSpec with MustMatchers {
"Package Object" must "return baz" in {
Run.run must equal ("baz")
}
}
on run scope the value the output is "bar" as expected and on test scope the test cases succeed since com.example.testbed.foo value is "baz"
Is this just a quirk of SBT or is it a well defined sbt trait?. I currently use this behaviour for dependency injection by defining my module bindings for production and test in their corresponding package objects. This is an advisable approach? and if not why?