Unable to use value classes for `answers` in idiomatic API
See original GitHub issueWhen running the following code on 1.6.2:
package com.example.test
import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito }
import org.scalatest.{ FlatSpec, Matchers }
class AnyValMockReproSpec extends FlatSpec with Matchers with IdiomaticMockito with ArgumentMatchersSugar {
val WString = "foo"
it should "return a prefixed value for `any` mock" in {
val decoder = mock[Decoder]
decoder.decode(any[ValueClass]) answers ((v: ValueClass) => v.value)
val tested = new Prefixer(decoder)
tested.prefix(ValueClass(WString)) should be(s"prefix-$WString")
}
it should "return a prefixed value for `*` mock" in {
val decoder = mock[Decoder]
decoder.decode(*) answers ((v: ValueClass) => v.value)
val tested = new Prefixer(decoder)
tested.prefix(ValueClass(WString)) should be(s"prefix-$WString")
}
}
case class ValueClass(value: String) extends AnyVal
trait Decoder {
def decode(v: ValueClass): String
}
class Prefixer(decoder: Decoder) {
def prefix(v: ValueClass): String = s"prefix-${decoder.decode(v)}"
}
the result is the following:
AnyValMockReproSpec:
- should return a prefixed value for `any` mock *** FAILED ***
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.test.ValueClass
at com.example.test.AnyValMockReproSpec.$anonfun$new$2$adapted(AnyValMockReproSpec.scala:11)
at org.mockito.package$.$anonfun$functionToAnswer$1(mockito.scala:30)
at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
at org.mockito.stubbing.ScalaAnswer$$anon$2.answer(ScalaAnswer.scala:13)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:39)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:96)
at org.mockito.internal.handler.ScalaMockHandler.handle(ScalaMockHandler.scala:37)
at org.mockito.internal.handler.ScalaNullResultGuardian.handle(ScalaNullResultGuardian.scala:11)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:35)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:61)
...
- should return a prefixed value for `*` mock *** FAILED ***
java.lang.NullPointerException:
at com.example.test.AnyValMockReproSpec.$anonfun$new$3(AnyValMockReproSpec.scala:20)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FlatSpecLike$$anon$5.apply(FlatSpecLike.scala:1682)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.FlatSpec.withFixture(FlatSpec.scala:1685)
It appears the function serving as the mocked method receives an unwrapped value that is then cast into a value class.
The second case might actually be unrelated to this problem (and may be another issue), included nevertheless for completeness’ sake.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Which C++ idioms are deprecated in C++11? - Stack Overflow
When you pass 0 to a template<class T> func(T) T gets deduced as an int and not as a null pointer constant. And...
Read more >Effective Go - The Go Programming Language
Values ; Interfaces and other types: Interfaces: Conversions: Interface conversions and ... This document gives tips for writing clear, idiomatic Go code.
Read more >Useful Kotlin Idioms You Should Know - The JetBrains Blog
Let's start with data classes, which are probably one of the most popular features of Kotlin. If you need a class to hold...
Read more >Errors | Cloud APIs - Google Cloud
These cover the most common needs for API errors, such as quota failure and invalid parameters. Like error codes, developers should use these...
Read more >Best Practices for Unit Testing in Kotlin - Philipp Hauer's Blog
Recap: What is Idiomatic Kotlin Code? Avoid Static and Reuse the Test Class Instance; Change the Lifecycle Default for Every Test Class; Use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mikolak-net That’s all right, otherwise I’d also have to change my comment 😃 I’ll try to fix this value class issue as soon as I can, thanks for reporting it!
That was fast, thanks!