2

I'm trying to mock Cassandra ScalaGettableData object using scalamock. I need to mock the following method:

def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name)

TypeConverter is a Trait and has implicit implementations such as:

implicit object StringConverter extends TypeConverter[String]

In my code I'm calling

scalaGettableData.getMap[String, String]("myMap")

and I guess it's implicitly converted to

scalaGettableData.getMap[StringConverter, StringConverter]("myMap")

My Test code is as following :

val cassandraRow1 = mock[ScalaGettableData]
(cassandraRow1.getMap[String, String] _).expects("localizations_config").returning(Map("key1" -> "value1"))`

But I'm getting compilation error:

Error:(28, 26) _ must follow method; cannot follow (name: String)(implicit evidence$3: com.datastax.spark.connector.types.TypeConverter[String], implicit evidence$4: com.datastax.spark.connector.types.TypeConverter[String])Map[String,String] <and> (index: Int)(implicit evidence$3: com.datastax.spark.connector.types.TypeConverter[String], implicit evidence$4: com.datastax.spark.connector.types.TypeConverter[String])Map[String,String]

How am I supposed to mock this method ?

Gridou
  • 109
  • 7

1 Answers1

5

Maybe this example helps:

"implicit parameters" should "be mockable" in {
  trait Bar[T]
  trait Foo {
    def getMap[K : Bar](name: String): Int
  }

  val m = mock[Foo]
  (m.getMap[Long](_: String)(_: Bar[Long])) expects(*, *) returning 42 once()

  implicit val b = new Bar[Long] {}
  m.getMap("bar")
}

Effectively, the type parameter K : Bar gets converted by the Scala compiler to a second parameter list, which is mocked out explicitely in this example with (_: Bar[Long]).

Philipp
  • 967
  • 6
  • 16
  • While this answer may solve the problem, you can improve the quality of your answer by explaining why or how the code works. Sort of a `teach a man to fish` thing. – Regular Jo Mar 05 '17 at 08:44
  • :) Have 10 points. When replying to someone besides the author of the exact post (question or answer), make sure to tag them like @Username, there's even an auto-complete of participating users. – Regular Jo Mar 06 '17 at 00:27