3

Scalamock reject my mocking attempt by saying that it doesnt support yet more than 22 methods.

The reason is because there are more than 22 methods, all together, in the class I am trying to mock (2 are mine, 20+ are mixed in (from Akka Json Support)).

Any ways to get round this limitation that doesnt involve rethinking the mixing part ?

I used it this way, with scalatest 3.0.2 : override val apiClient: ApiClient = mock[ApiClient] (apiClient.getById _).when(15538).returns("data")

Thank you !

David Puleri
  • 144
  • 8
  • Unfortunately, I think not because the restriction of 22 parameters to a function/method is coming from deep within the bowels of the [Scala standard library](http://www.scala-lang.org/api/2.12.0/scala/Function22.html). Although I just came across this [SO](http://stackoverflow.com/questions/20258417/how-to-get-around-the-scala-case-class-limit-of-22-fields) which might help. – davidrpugh Apr 12 '17 at 07:18

2 Answers2

3

I assume you don't actually want to test those JSON and other mixin functions, so I would suggest creating an abstract trait that defines your new, testable signatures and mixing it into your new class. This way you wouldn't need to change your design, and your clients of this ApiClient class could even decouple fully by using the trait type.

trait MyFunctionality {
  def foo(): Unit
  def somethingElse(i: Int): Int
}

class ApiClient extends Baseclass with Stuff with MoreStuff with MyFunctionality {
  // ...
}

then

val m = mock[MyFunctionality]
(m.foo _).expects().once()
// etc

That way you additionally guard against any code being run in your class (or baseclass) constructors during unit test time. Hope that helps.

Philipp
  • 967
  • 6
  • 16
0

I came out with the same solution at the end but I dont really like the noise it add to my very concise class. "C'est la vie" :)

David Puleri
  • 144
  • 8