1

I'm trying to use akka.http.scaladsl.testkit.responseAs in order to test some endpoints, but I can't figure out how to handle the marshalling/unmarshalling process of a org.joda.time.DateTime object. For instance, consider the case class below:

case class ConfigEntity(id: Option[Int] = None, description: String, key: String, value: String, expirationDate: Option[DateTime] = None)

Also, consider the following route test:

"retrieve config by id" in new Context {
  val testConfig = testConfigs(4)
  Get(s"/configs/${testConfig.id.get}") ~> route ~> check {
    responseAs[ConfigEntity] should be(testConfig)
  }
}

When I run "sbt test", the code does not compile, throwing the following error: "could not find implicit value for evidence parameter of type akka.http.scaladsl.unmarshalling.FromResponseUnmarshaller[me.archdev.restapi.models.ConfigEntity]"

I know the message is very self explanatory, but I still don't know how to create the implicit FromResponseUnmarshaller that the code is complaining about.

My code is based in this example: https://github.com/ArchDev/akka-http-rest

I'm just creating some new entities and trying to play around...

Thanks in advance.

goiaba
  • 11
  • 1

1 Answers1

0

This project uses CirceSupport. That means that you need to provide a Circe Decoder for the compiler to derive the Akka Http Unmarshaller.

Put the Decoder in scope:

case class ConfigEntity(id: Option[Int] = None, description: String, key: String, value: String, expirationDate: Option[DateTime] = None)

implicit val decoder = Decoder.decodeString.emap[DateTime](str =>
  Right(DateTime.parse(str))
)

"retrieve config by id" in new Context {
  val testConfig = testConfigs(Some(4))
  Get(s"/configs/${testConfig.id.get}") ~> route ~> check {
    responseAs[ConfigEntity] should be(testConfig)
  }
}

Obviously, you must deal with the possible exceptions trying to parse your DateTime and return Left instead Right...

I must say that I always use SprayJsonSupport for Akka Http, and this is the first time I´ve seen CirceSupport.

Hope this helps.

Emiliano Martinez
  • 4,073
  • 2
  • 9
  • 19
  • Thanks for the answer! I already have one of that. I'm able to send http requests when I execute `sbt run` and all works well (the config entity is persisted in the database). But when I execute `sbt test`, for some reason, the compiler does not find the implicit. – goiaba Jun 22 '17 at 21:30
  • Check that you don´t have more than one Decoder for DateTime in scope. Maybe the compiler has some ambiguity problem in resolution of implicits executing the tests – Emiliano Martinez Jun 23 '17 at 08:33