3

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 ?

1

1 Answer 1

6

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]).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
:) 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.