You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin中使用Gatling?类型不匹配编译错误求助

Gatling性能测试代码编译错误解决办法

你编写的Gatling模拟代码出现以下编译错误:

类型不匹配。
需要:List<io.gatling.core.structure.PopulationBuilder!>!
找到:io.gatling.javaapi.core.PopulationBuilder

错误原因

这是由于混合使用Gatling的Scala API与Java API导致的类型冲突:success.injectOpen(...)返回的是Java API的PopulationBuilder实例,但Scala版本的setUp方法期望接收Scala API的PopulationBuilder列表。

修正方案

统一使用Scala API语法调整代码,具体修改如下:

private val httpProtocol = http
    .baseUrl(apiGatewayUrl)
    .acceptHeader("application/json")
    .header("A", "foo")
    .header("B", "bar")
    .header("C", "3")

private val success = scenario("successful scenario")
    .exec(http("collector")
        .post("/v1/events")
        .body(StringBody("""
            {
             "body": "your-event-content"
        """.trimIndent())))

init {
    setUp(
       success.inject(
           constantUsersPerSec(5.0).during(5.seconds),
           rampUsers(10).during(10.seconds),
           constantUsersPerSec(10.0).during(20.seconds)
       )
    ).protocols(httpProtocol)
}

关键修改点

  • 替换injectOpen为Scala API的inject方法
  • 给during方法的参数添加显式时间单位(如.seconds),Scala API要求必须指定
  • 在protocols()中传入定义好的httpProtocol,原代码未传参会导致协议不生效
  • 修正请求体的JSON格式(原代码中body字段格式不合法,补充引号和示例值避免后续运行错误)

内容的提问来源于stack exchange,提问作者CyberPunk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:12:19