mocking methods which use ClassTag in scala using scalamock
Asked Answered
D

1

3

My first question, question, was answered but it uncovered another issue I am having. Here is the scenario.

Example code (expanded from previous question)

A 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 concrete component implementations (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 usage of component impl somewhere in 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 out provider in order to isolate repository test. This does not work, the following execption is thrown when test is run. I expect it is because of ClassTag usage because when I create another sample which does not use ClassTag, it works fine.

org.scalamock.MockFunction2 cannot be cast to org.scalamock.MockFunction1 java.lang.ClassCastException: org.scalamock.MockFunction2 cannot be cast 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)
  }
}

Unit Test2 does work but is hard 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)
  }
}

Unit Test3 does work but - used just to test ClassTag implemenation:

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 ClassTag wrong or is the mock not able to properly mock it?

Dicks answered 20/11, 2014 at 18:20 Comment(0)
H
4

This is similar to: ScalaMock User Guide: Mocking methods with implicit params - there is an implicit ClassTag parameter, so you have to convince Scala compiler that find[T](id:Int)(m: ClassTag[T]) should be converted to MockFunction2

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)
  }
}
Houseboat answered 21/11, 2014 at 7:6 Comment(1)
Ahh subtle, thanks that fixed it. Not sure if I like it though :) - I feel like I need to think more like a compiler and less like a developer.Dicks

© 2022 - 2024 — McMap. All rights reserved.