Getting Started with Gatling in Java Using Maven

Samuel Catalano
The Fresh Writes
Published in
2 min readSep 20, 2023

--

How to Use Gatling in Java Using Maven

Gatling is a popular open-source performance testing tool that can be used to test both web-based and API applications. It is written in Scala, but it also supports Java, starting from version 3.7. In this article, we will show you how to use Gatling in Java using Maven.

Prerequisites

  • Java 8 or higher
  • Maven 3 or higher
  • Gatling 3.7 or higher

Creating a Gatling Project in Java

To create a Gatling project in Java, you can use the following steps:

1. Create a new directory for your project.

2. Initialize a new Maven project in the directory using the following command:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

3. Enter the following values when prompted:

  • Group Id: Your group ID
  • Artifact Id: Your artifact ID
  • Version: Your project version

4. Once the Maven project has been created, open the pom.xml file in a text editor.

5. Add the following dependency to the pom.xml file:

<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-test-framework</artifactId>
<version>${gatling.version}</version>
</dependency>

6. Save the pom.xml file.

Creating a Gatling Simulation in Java

To create a Gatling simulation in Java, you can use the following steps:

  1. Create a new package called simulations in the src/test/java directory.
  2. Create a new class in the simulations package called MySimulation.java.
  3. Extend the Simulation class in the MySimulation class.
  4. Implement the setUp() and run() methods in the MySimulation class.

The setUp() method is used to initialize the simulation. The run() method is used to define the behaviour of the simulation.

Running a Gatling Simulation

To run a Gatling simulation, you can use the following command:

mvn gatling:test

This will start the Gatling simulator and run the simulation defined in the MySimulation class.

Reporting

Gatling generates a detailed report of the simulation results. The report can be found in the target/gatling directory.

Example

The following example shows a simple Gatling simulation in Java:

package simulations;

import io.gatling.core.scenario.Simulation;
import io.gatling.http.protocol.HttpProtocolBuilder;
import io.gatling.protocol.http.HttpRequestDefinition;

public class MySimulation extends Simulation {

private HttpProtocolBuilder httpProtocol = http
.baseUrl("http://localhost:8080");

@Override
public void setUp() {
scenario("MyScenario")
.exec(httpProtocol
.get("/")
.build());
}

@Override
public void run() {
setUp().injectOpen(
rampUsers(10)
.during(Duration.ofSeconds(10))
).start();
}
}

This simulation will send 10 concurrent requests to the / endpoint of the application running on localhost:8080.

To run the simulation, simply execute the following command:

mvn gatling:test

This will start the Gatling simulator and run the simulation.

Once the simulation is complete, you can view the report in the target/gatling directory.

Conclusion

Gatling is a powerful performance testing tool that can be used to test both web-based and API applications. Gatling supports Java, Scala, and Kotlin.

In this article, I showed you how to use Gatling in Java using Maven. I also showed you how to create a simple Gatling simulation in Java.

Happy coding ;)

--

--

Samuel Catalano
The Fresh Writes

Samuel is a Software Engineer from Brazil with main interests in Java, Spring Boot, Quarkus, Microservices, Docker, Databases, Kubernetes, and Clean Code