Java 21下Karate集成Gatling遇「No Simulations to Run」错误
解决Karate-Gatling集成时「No simulations to run」错误
问题根源
这个错误本质是Gatling找不到可执行的仿真类,核心原因有两个:
- Scala仿真文件放在了资源目录,未被正确编译
- Maven插件配置路径错误,且插件未实际激活
分步修复方案
1. 调整项目结构(关键)
Scala源码不属于资源文件,必须移出src/test/java/resources,放到标准的Scala测试源码目录:
karate/ ├── src/ │ ├── test/ │ │ ├── java/ │ │ │ ├── resources/ │ │ │ │ ├── performance/ │ │ │ │ │ ├── demoTest.feature # feature文件保留在resources │ │ ├── scala/ │ │ │ ├── performance/ │ │ │ │ ├── PerfTest.scala # 把Scala文件移到这里
2. 修正Maven插件配置
原配置仅在pluginManagement中定义了插件版本,但未实际启用;同时simulationsFolder指向了错误的资源目录,需要调整:
<build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <!-- 新增plugins节点,激活插件 --> <plugins> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling.plugin.version}</version> <configuration> <!-- 指向Scala测试源码根目录 --> <simulationsFolder>src/test/scala</simulationsFolder> <!-- 可选:直接指定要运行的仿真类,避免全量扫描 --> <simulationClass>performance.PerfTest</simulationClass> </configuration> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.9.2</version> <executions> <execution> <goals> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-Jbackend:GenBCode</arg> <arg>-Jdelambdafy:method</arg> <arg>-target:jvm-21</arg> <arg>-deprecation</arg> <arg>-feature</arg> <arg>-unchecked</arg> <arg>-language:implicitConversions</arg> <arg>-language:postfixOps</arg> </args> <!-- 指定Scala源码目录,确保插件能找到你的仿真文件 --> <sourceDir>src/test/scala</sourceDir> </configuration> </execution> </executions> </plugin> </plugins> <!-- 保留原pluginManagement内容不变 --> <pluginManagement> ... </pluginManagement> </build>
3. 修正Scala文件中的Feature路径
原代码里的feature路径多写了resources/,因为resources目录下的文件会直接加到classpath根路径,修改为:
val create = scenario("demo").exec(karateFeature("classpath:performance/demoTest.feature"))
4. 重新执行命令
清理并重新编译运行:
mvn clean test-compile gatling:test
验证要点
- 执行
test-compile后,检查target/test-classes/performance/目录下是否有PerfTest.class文件,确认Scala文件已被编译 - 若仍报错,加
-X参数查看详细日志:mvn clean test-compile gatling:test -X,确认Gatling插件的扫描路径是否正确
内容的提问来源于stack exchange,提问作者Mark Hughes
相关产品推荐
相关产品推荐

