如何在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
相关产品推荐
相关产品推荐

