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

如何通过Java代码运行Gatling测试并向Simulation传递测试URL参数(Maven环境)

Running Gatling Tests with Maven, Java Code, and Parameterized Target URLs

Got it, let's break down exactly how to set this up—from Maven configuration to passing your target URL (and other parameters) to your Gatling Simulation via Java code.

1. Maven Project Setup

First, add the necessary Gatling dependencies and Maven plugin to your pom.xml. This lets you build and run tests smoothly:

<dependencies>
    <!-- Gatling Java DSL -->
    <dependency>
        <groupId>io.gatling</groupId>
        <artifactId>gatling-java</artifactId>
        <version>3.11.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- Gatling Maven Plugin to run tests -->
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>3.11.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

(Note: Use the latest Gatling version available if you need to update the version number.)

2. Create a Parameterized Gatling Simulation (Java)

Next, write your Simulation class in Java, designed to accept external parameters like the target URL. We'll use system properties or direct argument passing to fetch the value:

import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;

public class ParameterizedSimulation extends Simulation {

    // Fetch target URL from system property, with a default fallback
    private final String targetUrl = System.getProperty("targetUrl", "https://default-api.com");

    private final HttpProtocolBuilder httpProtocol = http
            .baseUrl(targetUrl) // Use the parameterized base URL
            .acceptHeader("application/json");

    private final ScenarioBuilder scn = scenario("Sample API Test")
            .exec(http("GET Request")
                    .get("/endpoint")); // Adjust your endpoint as needed

    {
        setUp(
                scn.injectOpen(atOnceUsers(10)) // Configure your load profile here
        ).protocols(httpProtocol);
    }
}

3. Run Gatling Tests via Java Code

You can create a standalone Java runner class to trigger your Simulation programmatically. This gives you flexibility to pass parameters directly from your code:

import io.gatling.app.Gatling;
import io.gatling.core.config.GatlingPropertiesBuilder;

import java.util.Map;

public class GatlingTestRunner {
    public static void main(String[] args) {
        // Grab target URL from command line args, or use default
        String targetUrl = args.length > 0 ? args[0] : "https://default-api.com";

        GatlingPropertiesBuilder props = new GatlingPropertiesBuilder();
        props.simulationClass("com.yourpackage.ParameterizedSimulation")
                .systemProperties(Map.of("targetUrl", targetUrl)); // Pass parameter as system property

        Gatling.fromMap(props.build());
    }
}

4. Passing Parameters: Two Common Ways

Option 1: Command Line with Maven

If you run tests via Maven, pass the target URL as a system property:

mvn clean test -DtargetUrl=https://your-target-api.com

This will override the default value in your Simulation class.

Option 2: Programmatically via Java Runner

When using the GatlingTestRunner above, just pass the URL as a command line argument:

java -cp target/test-classes:target/classes com.yourpackage.GatlingTestRunner https://your-target-api.com

Or hardcode it in the runner (useful for local testing, though not ideal for dynamic scenarios).

Quick Tips

  • Always add a default value for parameters so your tests can run without explicit input.
  • For more complex parameters (like user counts, test durations), repeat the same pattern—use System.getProperty or pass them via the Map in the runner.
  • Tweak the load profile (like atOnceUsers) in your Simulation to match your testing needs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:57:26