Mocking methods that use ClassTag in scala using scala
My first question, question , was answered, but it found another problem I am experiencing. Here's the script.
Sample code (extended from the previous question)
Model:
case class User (first: String, last: String, enabled: Boolean)
Component definition:
trait DataProviderComponent {
def find[T: ClassTag](id: Int): Try[T]
def update[T](data: T): Try[T]
}
One of the specific implementations of the components (updated implementation):
class DbProvider extends DataProviderComponent {
override def find[T: ClassTag](id: Int): Try[T] = {
Try {
val gson = new Gson()
val testJson = """{"first": "doe", "last": "jane", "enabled": false}"""
gson.fromJson(testJson, implicitly[ClassTag[T]].runtimeClass).asInstanceOf[T]
}
}
override def update[T](data: T): Try[T] = ???
}
Implicit use of an impl component somewhere in the system:
implicit val provider = new DbProvider()
class UserRepsitory(implicit provider: DataProviderComponent) {
def userEnabled(id: Int): Boolean = {
val user = provider.find[User](id)
user.isSuccess && user.get.enabled
}
}
Unit Test1 trying to mock the provider to isolate the test repository. It won't work, when the test is executed, the next execution is called. I expect this is due to the use of the ClassTag, because when I create another sample that does not use the ClassTag, it works fine.
org.scalamock.MockFunction2 could not be passed to org.scalamock.MockFunction1 java.lang.ClassCastException: org.scalamock.MockFunction2 could not be passed to org.scalamock.MockFunction1
class UserRepository$Test1 extends FunSuite with Matchers with MockFactory {
test("invalid data request should return false") {
implicit val mockProvider: DataProviderComponent = mock[DataProviderComponent]
(mockProvider.find[User] _).expects(13).returns(Failure[User](new Exception("Failed!")))
val repo = new UserRepsitory()
repo.userEnabled(13) should be (false)
}
}
Test2 module works, but it is difficult to maintain and requires more code:
class UserRepository$Test2 extends FunSuite with Matchers with MockFactory {
test("invalid data request should return false") {
class FakeProvider extends DataProviderComponent {
override def find[T:ClassTag](id: Int): Try[T] = Failure[T](new Exception("Failed!"))
override def update[T](data: T): Try[T] = ???
}
implicit val provider = new FakeProvider()
val repo = new UserRepsitory()
repo.userEnabled(13) should be (false)
}
}
The Test3 module works, but is only used to test the implementation of the ClassTag:
class UserRepository$Test3 extends FunSuite with Matchers with MockFactory {
test("prove sut works") {
implicit val provider = new DbProvider()
val repo = new UserRepsitory()
val user = repo.userEnabled(13)
println(user.toString)
}
}
Am I using the ClassTag incorrectly or the layout is not able to mock it correctly?
source to share
It's like: ScalaMock User's Guide: Forging Methods with Implicit Parameters - there is an implicit parameter ClassTag
, so you need to convince Scala which find[T](id:Int)(m: ClassTag[T])
should be converted toMockFunction2
The following code works with ScalaMock 3.2:
package com.paulbutcher.test.mock
import org.scalamock.scalatest.MockFactory
import org.scalatest.{ FlatSpec, ShouldMatchers }
import scala.reflect.ClassTag
import scala.util.{ Failure, Try }
case class User(first: String, last: String, enabled: Boolean)
trait DataProviderComponent {
def find[T: ClassTag](id: Int): Try[T]
def update[T](data: T): Try[T]
}
class UserRepsitory(implicit provider: DataProviderComponent) {
def userEnabled(id: Int): Boolean = {
val user = provider.find[User](id)
user.isSuccess && user.get.enabled
}
}
class ClassTagTest extends FlatSpec with ShouldMatchers with MockFactory {
behavior of "ScalaMock"
it should "handle mocking methods with class tags" in {
implicit val mockProvider: DataProviderComponent = mock[DataProviderComponent]
(mockProvider.find[User](_: Int)(_: ClassTag[User])).expects(13, *).returns(Failure[User](new Exception("Failed!")))
val repo = new UserRepsitory()
repo.userEnabled(13) should be(false)
}
}
source to share