score:2
the issue is that extension methods are basically a syntactic sugar.
i will be explaining using an example.
trait foo
implicit class fooimplicit(foo: foo) {
def bar: string = "bar
}
foo.bar
is translated to
new fooimplicit(foo).bar
so mocking:
mockito.when(foo.bar).thenreturn("bad")
becomes:
mockito.when(new fooimplicit(foo).bar).thenreturn("bad")
note how foo.bar
is treated and that's the issue.
is it possible to achieve what i want in the current setup?
no, i don't think it is possible in the current setup.
if not, how should i refactor my code
the only way to achieve that is to use implicit conversions rather than implicit classes.
i will be showing an example how this could be achieved:
trait foo {
def bar: string
}
object implicitfoo {
object implicits {
implicit footofooimplicit(foo: foo): fooops = new fooimplicit(foo)
class fooimplicit(foo: foo) {
def bar: string = "bar"
}
}
}
and your test
import org.scalatest.wordspec
import org.mockito.mockitosugar
class myspec extends wordspec with mockitosugar {
"my mock" should {
"handle methods from implicit classes" in {
val fooops = mock[fooimplicit]
implicit footoops(foo: foo): fooimplicit = fooops
val foo = mock[foo]
when(foo.bar) thenreturn "bad" // works
}
}
}
in your production you need to get an implicit parameter of the shape foo => fooimplicit
so when you call that method from the test the actual implicit mock is provided...
if i test in a different class the methods available in the implicit class, does it make sense to test the readsometable and the mainmethod inside myobject?
i do not think that you require to test the readsometable and the mainmethod inside myobject.but other way round is true.
let me know if it helps!!
Source: stackoverflow.com
Related Query
- Test class with implicit class using ScalaMock
- Using Scala class in Java test with Gradle
- Test a nested method call on a mocked class using ScalaMock
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Class broken error with Joda Time using Scala
- PowerMockito throwing ClassNotPreparedException even with @PrepareForTest using Scala Test
- Using implicit class to override method
- Scala: Implicit evidence for class with type parameter
- Difference between conversion with implicit function and implicit class in Scala
- Specs2: how to test a class with more than one injected dependency?
- Implicit jsonFormat for case class with varargs
- Json implicit format with recursive class definition
- Using new with Scala final case class
- spark convert dataframe to dataset using case class with option fields
- How to automatically generate a function to match a sealed case class family with implicit instances?
- Constrain a class with implicit evidence
- Creating a more specific implicit using a structural type with Scala
- How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?
- Using @Transactional on actors class definition with Akka 2.X.X?
- Convert JSON to case class with a nested objects using Scala/Play
- How to define implicit class for Traversable with CanBuildFrom?
- Using method name "x" with implicit conversions in Scala
- Error: "could not find implicit value for parameter readFileReader" trying to save a file using GridFS with reactivemongo
- Mock partially a class with scalamock
- Dataframe.rdd.count throws No applicable constructor/method found for actual parameters when using case class with List
- scala implicit class method type mismatch in reduce vs non-implicit method with function currying
- How to extend class with implicit parameter in Scala
- compile error with implicit class method named "clone"
- PlaySpec test class not picked up if using injection
- How do you use scalamock to mock a class with constructor parameters
More Query from same tag
- TCP server handling multiple connections
- Why Transactors have been deprecated in Akka 2.3
- Kubernetes spark-submit
- Explanation for "illegal cyclic reference" involving implicits
- Why Akka shutdowns dispatcher, when there is no tasks?
- How to use multiple counters in Flink
- Scala type inference working with Slick Table
- Changing <title> with Lift
- How to parse json list or array in scala for play framework 2.2
- Not enough arguments for method unmarshal: (implicit evidence$1: spray.httpx.unmarshalling.FromResponseUnmarshaller
- Spark (Scala) sqlDataFrame Joins not working as expected
- Parsing scala 3 code from a String into Scala 3 AST at runtime
- Scala - How to access nested columns in a dataframe?
- Why Am I getting Nullpointer Exception
- Garbage collection time very high in spark application causing program halt
- Java/Scala library for user login and authentication
- How can one set up a Play Framework 404 (OnHandlerNotFound) page in Play 2.2?
- Extractor does not return exception
- Cassandra setFetchSize and number of rows returned
- Message delivery sequence in akka actors
- How to increase RAM for embedded-kafka?
- Insert one method invocation into many other methods
- What is the execution order (sequential or concurrent) of futures inside of for comprehension?
- How do I deserialize a JSON response when making a http request in akka?
- How to get the SQL representation for the query logic of a (derived) Spark DataFrame?
- Opening modal in list in Scala HTML
- Slick 3 how can I get the default configuration in the application file
- Type overriding for parametrized method in scala
- Using map or flatMap on a pattern matched output value
- How to fold a sequence with a writer monad embedding a disjunction using combinators?