如何在Scala中编写AWS lamda单元测试用例?

时间:2018-12-08 14:21:28

标签: scala aws-lambda aws-serverless

我们如何为AWS lamda无服务器实现单元测试用例。 我的代码是

    object Test1 extends RequestHandler[APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent] with ResponseObjProcess {


  override def handleRequest(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = {
    var response = new APIGatewayProxyResponseEvent()
    val gson = new Gson

    val requestHttpMethod = input.getHttpMethod
    val requestBody = input.getBody
    val requestHeaders = input.getHeaders
    val requestPath = input.getPath
    val requestPathParameters = input.getPathParameters
    val requestQueryStringParameters = input.getQueryStringParameters
    val parsedBody = JSON.parseFull(requestBody).getOrElse(0).asInstanceOf[Map[String, String]]
    println(" parsedBody is:: " + parsedBody)

    val active = parsedBody.get("active").getOrElse("false")
    val created = parsedBody.get("created").getOrElse("0").toLong
    val updated = parsedBody.get("updated").getOrElse("0").toLong

    requestHttpMethod match {
      case "PUT" =>
        println(" PUT Request method ")
        //  insertRecords("alert_summary_report", requestBody)
        response.setStatusCode(200)
        response.setBody(gson.toJson("PUT"))
      case _ =>
        println("")
        response.setStatusCode(400)
        response.setBody(gson.toJson("None"))

    }
    response
  }
}

我尝试为上述代码实现单元测试用例。 下面的代码是:

test("testing record success case") {

    var request = new APIGatewayProxyRequestEvent();
    request.setHttpMethod(Constants.PUTREQUESTMETHOD)

    DELETEREQUESTBODY.put("id", "")

    request.setBody(EMPTYREQUESTBODY)
    request.setPathParameters(DELETEREQUESTBODY)

    println(s"body = ${request.getBody}")
    println(s"headers = ${request.getHeaders}")

    val response = ProxyRequestMain.handleRequest(subject, testContext)
    val assertEqual = response.getStatusCode.equals(200)
    assertEqual
  }

实际上,我收到了 response.getStatusCode = 400 错误的请求,但是测试用例通过了我该如何编写来处理这个问题。

1 个答案:

答案 0 :(得分:1)

我正在查看您的测试代码,但我不清楚您要通过断言要实现什么。我认为您可能混合了很多东西。在当前代码中,您有一个val,而不是断言。建议您查看相关文档,并研究可用的选项:

http://www.scalatest.org/user_guide/using_assertions

http://www.scalatest.org/user_guide/using_matchers