I want to mock this method
def zadd[V: ByteStringSerializer](key: String, scoreMembers: (Double, V)*): Future[Long]
Tried this
mock.zadd(anyString(), Seq((anyDouble(), any String()), (anyDouble(), anyString())): _*)
doesnt work because mockito says 3 matchers expected, but got 5 instead.
so I try using How to properly match varargs in Mockito
but, I cant even use the code listed in that example
ArgumentMatchers.<String>any() gets flagged as error in my IDE saying not recognized type String
mockito-scala supports varargs out-of-the-box, for example
import cats.implicits._
import org.mockito.ArgumentMatchersSugar
import org.mockito.cats.IdiomaticMockitoCats
import org.mockito.scalatest.IdiomaticMockito
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
trait Foo {
def zadd(key: String, scoreMembers: (Double, String)*): Future[Long]
}
class HelloSpec extends FlatSpec
with Matchers
with IdiomaticMockito
with IdiomaticMockitoCats
with ScalaFutures
with ArgumentMatchersSugar {
"Foo" should "mock varargs" in {
val foo = mock[Foo]
foo.zadd("", *) returnsF 42L
foo.zadd("bar", (7.7, "qux")) returnsF 89L
foo.zadd("", (1.1, "")).futureValue should be (42L)
foo.zadd("bar", (7.7d, "qux")).futureValue should be (89L)
}
}
where
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.8",
"org.mockito" %% "mockito-scala" % "1.5.13",
"org.mockito" %% "mockito-scala-scalatest" % "1.5.13",
"org.mockito" %% "mockito-scala-cats" % "1.5.13",
"org.typelevel" %% "cats-core" % "2.0.0-M4"
)
Related
I am transitioning from Java to Scala. I have written a simple test to render a view. Like :
import org.scalatestplus.play.PlaySpec
import org.scalatest._
import org.slf4j.LoggerFactory
import play.test.WithApplication
class EvTemplateTests extends PlaySpec{
implicit lazy val log = LoggerFactory.getLogger(getClass)
//run your test//run your test
"render eval template" in {
val html = views.html.index("Hello")
contentType(html) must equalTo("text/html")
contentAsString(html) must contain("Welcome to Play!")
}
}
When compiling, looks like it is not finding "index","contentType", "contentAsString" etc.Looks like, the project is using the libraries:
lazy val thirdPartyDependencies = Seq(
jdbc,
"com.typesafe.play" %% "anorm" % "2.4.0",
"com.typesafe.play" %% "play-mailer" % "3.0.1",
"com.microsoft.sqlserver" % "mssql-jdbc" % "6.4.0.jre8",
"io.swagger" %% "swagger-play2" % "1.5.0", // This version adds Play 2.4 support.
// ScalaTest+ Play (have to use non-release 1.4.0-M4 version for now as it is only compatible with Play 2.4)
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test",
"org.mockito" % "mockito-core" % "1.10.19" % "test"
)
May I get any insight?
You can start from here:
import controllers.AssetsFinder
import org.specs2.mutable.Specification
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
class EvTemplateTests
extends Specification
with DefaultAwaitTimeout
with FutureAwaits
with Results {
val application: Application = GuiceApplicationBuilder().build()
"render eval template" in new WithApplication(app = application) {
implicit val assetsFinder = app.injector.instanceOf[AssetsFinder]
val html = views.html.index("Hello")
contentType(html) mustEqual "text/html"
contentAsString(html) must contain("Hello")
}
}
add this after jdbc in LibraryDependencies: specs2 % Test
Most important part of test setup here is implicit AssetFinder that derived from GuiceApplicationBuilder:
val application: Application = GuiceApplicationBuilder().build()
AssetFinder is really important part of view testing in PlayFramework
Is it possible to compare only part of string in property of case class.
Using scala specs2 lib.
Is there is a possibility to write such matcher?
CaseClass(property = Prop("very long string")) must beEqualTo(CaseClass(property = Prop("%long%")))
Try case class matchers like so
foo must matchA[Foo].property(_ must =~("long"))
Here is a working example
import scala.language.experimental.macros
import org.specs2._
import org.specs2.matcher.MatcherMacros
class QuickStartSpec extends Specification with MatcherMacros { def is = s2"""
The 'Case class matchers' should
match on a part of a string $e1
"""
def e1 = {
case class Foo(property: String)
val foo = Foo(property = "very long string")
foo must matchA[Foo].property(_ must =~("long"))
}
}
where
libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "4.6.0" % "test",
"org.specs2" %% "specs2-matcher-extra" % "4.6.0" % "test"
),
With standard matchers
myCaseInst.property.myString must be matching(".*long.*")
I'm trying to create Show Instance for my custom Config class.
The build.sbt file is -
name := "circe-demo"
version := "0.1"
scalaVersion := "2.11.12"
resolvers += Resolver.bintrayRepo("ovotech", "maven")
libraryDependencies += "io.circe" %% "circe-core" % "0.11.0"
libraryDependencies += "io.circe" %% "circe-parser" % "0.11.0"
libraryDependencies += "io.circe" %% "circe-generic" % "0.11.0"
libraryDependencies += "org.typelevel" %% "kittens" % "1.2.0"
libraryDependencies ++= Seq(
"is.cir" %% "ciris-cats",
"is.cir" %% "ciris-cats-effect",
"is.cir" %% "ciris-core",
"is.cir" %% "ciris-enumeratum",
"is.cir" %% "ciris-refined"
).map(_ % "0.12.1")
Complete code is -
import enumeratum.{Enum, EnumEntry}
sealed abstract class AppEnvironment extends EnumEntry
object AppEnvironment extends Enum[AppEnvironment] {
case object Local extends AppEnvironment
case object Testing extends AppEnvironment
case object Production extends AppEnvironment
override val values: Vector[AppEnvironment] =
findValues.toVector
}
import java.net.InetAddress
import scala.concurrent.duration.Duration
final case class ApiConfig(host: InetAddress, port: Int, apiKey: String, timeout: Duration)
import java.net.InetAddress
import cats.Show
import cats.derived.semi
import ciris.config.loader.AppEnvironment.{Local, Production, Testing}
import enumeratum.EnumEntry
import eu.timepit.refined.auto._
import eu.timepit.refined.types.string.NonEmptyString
import scala.concurrent.duration._
final case class Config(appName: NonEmptyString, environment: AppEnvironment, api: ApiConfig)
object Config {
implicit val showConfig: Show[Config] = {
implicit val showDuration: Show[Duration] =
Show.fromToString
implicit val showInetAddress: Show[InetAddress] =
Show.fromToString
implicit def showEnumEntry[E <: EnumEntry]: Show[E] =
Show.show(_.entryName)
// Show.show[Config](x => s"api = ${x.api} appName = ${x.appName} environment ${x.environment}")
semi.show
}
}
semi.show in the above code throws the below exception -
[error] /Users/rajkumar.natarajan/Documents/Coding/kafka_demo/circe-demo/src/main/scala/ciris/config/loader/Config.scala:32:5: ambiguous implicit values:
[error] both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
[error] and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
[error] match expected type cats.derived.MkShow[A]
[error] show
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error]
I'm new to functional programming using cats.
How can I resolve this exception.
Unfortunately error reporting when such complicated implicits and macros are involved is far from perfect. The message you see actually means that some required implicits for the real generator (MkShow.genericDerivedShowProduct in this case) have not been found and the search went back to some where basic stuff where there is an ambiguity. And the stuff that is missing is mostly very basic such as an implicit for Show[Int] or Show[String]. The simplest way to get them all is to import cats.implicits._ but that will also bring catsStdShowForDuration which is a Show[Duration]. But since it's implementation is really the same as your custom one, it is easier to remove your custom one. One more thing that is missing is Show[NonEmptyString] and it is easy to create one
implicit def showNonEmptyString: Show[NonEmptyString] = Show.show(nes => nes)
To sum up, when I define your showConfig as
implicit val showConfig: Show[Config] = {
import cats.implicits._
// is already defined in cats.implicits._
//implicit val showDuration: Show[Duration] = Show.fromToString
implicit val showInetAddress: Show[InetAddress] = Show.fromToString
implicit def showEnumEntry[E <: EnumEntry]: Show[E] = Show.show(_.entryName)
implicit def showNonEmptyString: Show[NonEmptyString] = Show.show(nes => nes)
// Show.show[Config](x => s"api = ${x.api} appName = ${x.appName} environment ${x.environment}")
semi.show
}
it compiles for me.
P.S. is there any good reason why you put your AppEnvironment under ciris.* package? I'd say that generally putting your custom code into packages of 3-rd party library is an easy way to mess things up.
Why can I not tell a mock that it should expect an instance of a class without explicitly giving the type? Here is what I mean:
val myClass = new MyClass(...)
val traitMock = mock[MyTrait]
(traitMock.mymethod _).expects(myClass).returning(12.3)
does not work, while
val myClass: MyClass = new MyClass(...)
val traitMock = mock[MyTrait]
(traitMock.mymethod _).expects(myClass).returning(12.3)
does work. How come the type can not be inferred?
My testing part in build.sbt is
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.0" % "test"
exclude("org.scala-lang", "scala-reflect")
exclude("org.scala-lang.modules", "scala-xml")
)
libraryDependencies += "org.scalamock" %% "scalamock-scalatest-support" % "3.3.0" % "test"
Since I was asked for MyClass (it is SpacePoint here):
trait SpacePoint {
val location: SpaceLocation
}
val sp = new SpacePoint {
override val location: SpaceLocation = new SpaceLocation(DenseVector(1.0, 1.0))
}
So actually it works for me. Let me mention that type inference in the code:
val myClass = new MyClass(...)
has nothing to do with ScalaMock but is guaranteed by scala itself. Below I will specify working sample with library versions and sources of the classes.
Testing libraries:
"org.scalatest" %% "scalatest" % "2.2.4" % "test",
"org.scalamock" %% "scalamock-scalatest-support" % "3.2.2" % "test"
Source code of classes:
class MyClass(val something: String)
trait MyTrait {
def mymethod(smth: MyClass): Double
}
Source code of test:
import org.scalamock.scalatest.MockFactory
import org.scalatest.{Matchers, WordSpec}
class ScalamockTest extends WordSpec with MockFactory with Matchers {
"ScalaMock" should {
"infers type" in {
val myClass = new MyClass("Hello")
val traitMock = mock[MyTrait]
(traitMock.mymethod _).expects(myClass).returning(12.3)
traitMock.mymethod(myClass) shouldBe 12.3
}
}
}
Hope it helps. Will be ready to update answer once you provide more details.
TL/DR: Checkout last two commits and run sbt clean compile: https://github.com/mirelon/akka-in-action/tree/json/chapter2
There is RestInterface.scala:
package com.goticks
import akka.actor._
import spray.routing._
import spray.http.StatusCodes
import spray.httpx.SprayJsonSupport._
import spray.routing.RequestContext
import akka.util.Timeout
import scala.concurrent.duration._
import scala.language.postfixOps
class RestInterface extends HttpServiceActor
with RestApi {
def receive = runRoute(routes)
}
trait RestApi extends HttpService { actor: Actor =>
import context.dispatcher
import com.goticks.TicketProtocol._
implicit val timeout = Timeout(10 seconds)
import akka.pattern.ask
def routes: Route =
path("events") {
get { requestContext =>
context.actorOf(Props[Resu]).ask(GetEvents)
}
}
}
class Responder(requestContext:RequestContext) extends Actor {
import TicketProtocol._
def receive = {
case Events(events) =>
requestContext.complete(StatusCodes.OK, events)
self ! PoisonPill
}
}
and Resu.scala (note that Resu is alphabetically after RestInterface)
package com.goticks
import akka.actor.Actor
class Resu extends Actor {
import TicketProtocol._
import spray.json._
def receive = {
case GetEvents => {
println(Event(event = "E").toJson)
}
}
}
object TicketProtocol {
import spray.json._
case class Event(event:String)
case object GetEvents
case class Events(events:List[Event])
object Event extends DefaultJsonProtocol {
implicit val format = jsonFormat1(Event.apply)
}
}
build.sbt:
name := "goticks"
version := "0.1-SNAPSHOT"
organization := "com.goticks"
scalaVersion := "2.11.1"
libraryDependencies ++= {
val akkaVersion = "2.3.4"
val sprayVersion = "1.3.1"
Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"io.spray" %% "spray-can" % sprayVersion,
"io.spray" %% "spray-routing" % sprayVersion,
"io.spray" %% "spray-json" % "1.2.6"
)
}
build.properties;
sbt.version=0.13.7
plugins.sbt:
resolvers += Classpaths.typesafeResolver
resolvers += "sbt-idea" at "http://mpeltonen.github.com/maven/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.10.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")
Compilation issue
When I run sbt clean compile, everything is ok. But when I refactor the Resu class into Ress (note that Ress is alphabetically before RestInterface) (also rename its file, the diff can be viewed here: https://github.com/mirelon/akka-in-action/commit/583ca801fb7d1564024eee2f98d57f03ecacc6e5), then there is a compilation error:
[error] /home/miso/IdeaProjects/akka-in-action/chapter2/src/main/scala/com/goticks/Ress.scala:12: Cannot find JsonWriter or JsonFormat type class for com.goticks.TicketProtocol.Event
[error] println(Event(event = "E").toJson)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
Clearly there is an implicit json writer missing. Could it be due to incorrect order of compiling? Could sbt compile the classes in alphabetical order, ignoring imports?
Yes, sbt always compiles in alphabetical order. The issue is sbt has no idea the dependencies between files until it runs the compiler.
What you're seeing is the scala compiler itself is dependent on the ordering of source files. Sbt always sorts source files so you can at least work around these issues by placing code in an ordering that works.
I'm not 100% sure why you're hitting such an issue from some implicits + macro hackery, but that could be wrong.