I had to do the same in order to test one endpoint (in the Integration test). So, the following code to get a response from a GET request in Scala. I am using Scala.io.Source to read from the endpoint and ObjectMapper to convert json to object.
private def buildStockMasterUrl(url:String, stockStatus:Option[String]) = { stockStatus match { case Some(stockStatus) => s"$url?stockStatus=${stockStatus}" case _ => url } } private def fetchBooksMasterData(stockStatus:Option[String]): util.ArrayList[BooksMasterData] = { val url: String = buildBooksMasterUrl("http://localhost:8090/books/rest/catalogue/booksMasterData",stockStatus) val booksMasterJson : String = scala.io.Source.fromURL(url).mkString val mapper = new ObjectMapper() apper.readValue(booksMasterJson,classOf[util.ArrayList[BooksMasterData]]) } case class BooksMasterData(id:String,description: String,category: String)
And here is my testing method for the same
test("validate booksMasterData resource") { val booksMasterData = fetchBooksMasterData(Option(null)) booksMasterData.size should be (740) }
Sanjay Bharwani Feb 07 '17 at 9:22 2017-02-07 09:22
source share