Test cases for file upload using akka-http in Scala

Reading Time: < 1 minute

Hello folks, In my previous blog, i explained how to upload a file using akka-http in Scala. Later, i got the queries about the test cases for the same. Therefore, in this blog I am going to explain the way of writing test cases for file upload.

First we need to add test dependency :

Full code :

Get full code here.

It’s done. Hope you enjoy it. In my next blog, I will create a basic application to handle multipart formdata (file + form fields) using akka-http in Scala. So Stay tuned !!

Happy Blogging !!!

Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

4 thoughts on “Test cases for file upload using akka-http in Scala1 min read

  1. Hi

    I have an Akka HTTP service like


    path( “file-upload”) {
    extractClientIP { ip =>
    optionalHeaderValueByName(Constants.AUTH) { auth =>
    (post & extractRequestContext) { request =>
    extractRequestContext {
    ctx => {
    implicit val materializer = ctx.materializer
    implicit val ec = ctx.executionContext
    val currentTime = TimeObject.getCurrentTime()
    fileUpload(“fileUpload”) {
    case (fileInfo, fileStream) =>
    val localPath = Configuration.excelFilePath
    val uniqueidString = “12345”
    val filename = uniqueidString + fileInfo.fileName
    val sink = FileIO.toPath(Paths.get(localPath) resolve filename)
    val writeResult = fileStream.runWith(sink)
    onSuccess(writeResult) { result =>
    result.status match {
    case Success(_) =>
    var excelPath = localPath + File.separator + uniqueidString + fileInfo.fileName
    var doc_count = itemsExcelParse(excelPath, companyCode, subCompanyId, userId)
    val currentTime2 = TimeObject.getCurrentTime()
    var upload_time = currentTime2 – currentTime
    val resp: JsValue = Json.toJson(doc_count)
    complete {
    val json: JsValue = Json.obj(“status” -> Constants.SUCCESS,
    “status_details” -> “null”, “upload_details” -> resp)
    HttpResponse(status = StatusCodes.OK, entity = HttpEntity(ContentType(MediaTypes.`application/json`), json.toString))

    case Failure(e) =>
    complete {
    val json: JsValue = Json.obj(“status” -> Constants.ERROR)
    HttpResponse(status = StatusCodes.BandwidthLimitExceeded, entity = HttpEntity(ContentType(MediaTypes.`application/json`), json.toString))

    am using test code like

    it should “be able to upload file” in {
    val file = new File(getClass.getResource(“E:\\Excel\\1122 gr.xls”).toString)
    val formData = Multipart.FormData.fromFile(“fileUpload”, ContentTypes.`application/octet-stream`, file, 100000)
    Post(s”/api/file-upload”, formData).withHeaders(oauthHeader) ~> createPORoute -> check {
    status shouldBe StatusCodes.OK
    responseAs[String] contains “File successfully uploaded”

    but am getting the issue

    [info] – should be able to upload file *** FAILED ***
    [info] java.lang.NullPointerException:
    [info] at ExcelfileServiceSpec$$anonfun$1.apply$mcV$sp(ExcelfileServiceSpec.scala:34)
    [info] at ExcelfileServiceSpec$$anonfun$1.apply(ExcelfileServiceSpec.scala:33)
    [info] at ExcelfileServiceSpec$$anonfun$1.apply(ExcelfileServiceSpec.scala:33)
    [info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
    [info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
    [info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    [info] at org.scalatest.Transformer.apply(Transformer.scala:22)
    [info] at org.scalatest.Transformer.apply(Transformer.scala:20)
    [info] at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1647)
    [info] at org.scalatest.Suite$class.withFixture(Suite.scala:1122)

    how can I resolve the above issue

Comments are closed.

%d bloggers like this: