java – Scala – 如何在多行字符串文字中使用变量

我想调用’myActionID’变量的值.我怎么做?
如果我将像“actionId”:1368201这样的静态值传递给myActionID然后它可以正常工作,但如果我使用“actionId”:${actionIdd}则会出错.

这是相关的代码:

class LaunchWorkflow_Act extends Simulation {

    val scenarioRepeatCount = 1
    val userCount = 1
    val myActionID = "13682002351"

    val scn = scenario("LaunchMyFile")
        .repeat (scenarioRepeatCount) {
            exec(session => session.set("counter", (globalVar.getAndIncrement+" "+timeStamp.toString())))
            .exec(http("LaunchRequest")
            .post("""/api/test""")
            .headers(headers_0)
            .body(StringBody(
                """{    "actionId": ${myActionID} ,
                "jConfig": "{\"wflow\":[{\"Wflow\":{\"id\": \"13500145349\"},\"inherit-variables\": true,\"workflow-context-variable\": [{\"variable-name\": \"externalFilePath\",\"variable-value\": \"/var/nem/nem/media/mount/assets/Test.mp4\"},{\"variable-name\": \"Name\",\"variable-value\": \"${counter}\"}]}]}"
                }""")))

            .pause(pause) 

        }
    }

setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)

一切正常如果我把值13682002351而不是myActionID.提前致谢.在Gatling中执行此脚本时我遇到此错误

ERROR i.g.http.action.HttpRequestAction – ‘httpRequest-3’ failed to
execute: No attribute named ‘myActionID’ is defined

解决方法:

Scala具有各种字符串插值机制(参见docs),可用于将变量嵌入字符串中.所有这些都可以与用于创建多行字符串的三重引号“”“结合使用.

在这种情况下,您可以使用:

val counter = 12
val myActionID = "13682002351"
val str = s"""{    
                "actionId": $myActionID ,
                "jConfig": "{\"wflow\":[{\"Wflow\":{\"id\": \"13500145349\"},\"inherit-variables\": true,\"workflow-context-variable\": [{\"variable-name\": \"externalFilePath\",\"variable-value\": \"/var/nem/nem/media/mount/assets/Test.mp4\"},{\"variable-name\": \"Name\",\"variable-value\": \"${counter}\"}]}]}"
              }"""

请注意s前置于字符串文字,并且美元符号前置于变量名称.

上一篇:Gatling-插件开发


下一篇:读取数据表中第m条到第n条的数据,SQL语句怎么写?