Discover Suite with constructor with default parameter

20 views
Skip to first unread message

Robert Elliot

unread,
Nov 30, 2015, 11:31:48 AM11/30/15
to scalatest-users
Hi,

I have a Suite with a constructor with default arguments as so:

class MyTests(environmentFactory: FixtureFactory[Environment] = EnvironmentFactory()) extends fixture.FunSuite {

  override type FixtureParam = Environment

  test
("hello world") { environment =>
   
...
 
}

  override protected def withFixture(test: OneArgTest): Outcome = {
   
environmentFactory.using { environment =>
      test
.apply(environment)
   
}
 
}

}

object EnvironmentFactory {
 
def apply():
FixtureFactory[Environment] = ???
}


I want this because the environment factory may optionally be a pool of environments, and I'd like to manage that pool at a higher level when running all my tests but use a non-pooled default version when running one test.

Unfortunately the scalatest SuiteDiscoveryHelper.isAccessibleSuite method doesn't find it as a Suite because Java reflection doesn't see it as having a no args constructor. Is there anything that could be done using Scala reflection to allow for this case? I've had a quick look at the Scala reflection API and it wasn't immediately obvious...

Wondering really if it's worth raising an issue for this or not, particularly as I don't know how to fix it myself! I'm sort of hoping that if it worked in the scalatest runner it would also work inside IntelliJ (which also refuses to run this class as a test standalone).

Thanks,
Rob

Bill Venners

unread,
Nov 30, 2015, 11:44:47 AM11/30/15
to scalate...@googlegroups.com
Hi Robert,

The way to achieve what you want is to not use default arguments. Just use two overloaded constructors instead, one that takes the environmentFactory (the primary constructor) and one that takes no arguments (an auxiliary constructor). The auxiliary then passes the EnvironmentFactory to the primary. Something like:

class MyTests(environmentFactory: FixtureFactory[Environment]) extends fixture.FunSuite {

  def this() = this(EnvironmentFactory())

  ...

This way MyTests does have a no-arg constructors, so it can be discovered by ScalaTest (and sbt, etc.).

Bill


--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Bill Venners
Artima, Inc.
http://www.artima.com

Robert Elliot

unread,
Nov 30, 2015, 2:16:28 PM11/30/15
to scalatest-users
Thanks Bill, and yes, that works great.

Just another scala gotcha I suppose - I'd assumed that the default arguments version was just a shorthand for the version with two explicit constructors, but evidently not!

Cheers,
Rob
Reply all
Reply to author
Forward
0 new messages