-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matching an AnyVal with private constructor #391
Comments
Scala version? (iirc on each version I use a different |
|
Well, here your code doesn't make it any easy, as there's no way for mockito to know how to construct a "default" value for it (value classes need a default value cause a reference to a value class can't ever be Luckily the design of mockito-scala allows you to circumbent this (yay to past me!!) XD So there's this type class In any case, the solution is pretty simple, provide an final case class CountryCode private (value: String) extends AnyVal
object CountryCode extends LazyLogging {
def apply(code: String): Either[String, CountryCode] = { ... }
}
}
implicit val defaultValueProvider: DefaultValueProvider[CountryCode] =
new DefaultValueProvider[CountryCode] {
override def default: CountryCode = CountryCode("").value
}
foo.bar(any[CountryCode]).returns(baz) Let me know how it goes :) |
Oh wow, that's pretty neat and it worked! One "solution" I see is to use a different type, like On top of that, do you have control over original error message so it could point to the solution? |
Were all the params value classes? afaik, you only need to provide the types for value classes. |
Nope, only a single one but parameters become necessary because once I put |
Ohh, you can also make it not-implicit, it is just a param on the |
Right! I haven't thought of that :) Thanks. You can keep this issue open if you'd like to improve the error message, otherwise, feel free to close. |
I'm not sure if it's limited to AnyVals or applies to any class with a private constructor, but it seems to be impossible to match arguments of such type.
Pseudo-example:
fails compilations with
I haven't found a workaround for now
The text was updated successfully, but these errors were encountered: