I need help in converting a scala code to java

I’m new to Gatling and initially used Scala for the load test but now want to go ahead with Java as the compiler is much faster. And therefore I want to convert the below Scala code that I had written earlier to Java.

Scenarios in Loop

import io.gatling.http.Predef.*
import io.gatling.core.Predef.*
import io.gatling.core.structure.PopulationBuilder

import scala.collection.immutable.ArraySeq
import scala.collection.mutable
import scala.concurrent.duration.*
import scala.language.postfixOps

class personalDetails extends Simulation {

  // Protocol
  val httpProtocol = http.baseUrl("https://morpheus.uat.creditsaison.xyz")
    .header("requestingSub", "5317f163-448d-48f2-8f30-900cdd669288")
    .authorizationHeader("Basic QGRNMU46UEAkJFcwckQ=")

  var users = 100
  var i = 0

  // Scenario
  def scnList() = {
    var scnList = List.empty[PopulationBuilder]

    for (_ <- 0 to 2) yield {
      val injectionProfile = users match {
        case 100 => rampUsers(5).during(1)
        case 200 => rampUsers(10).during(1)
	case 300 => rampUsers(15).during(1)

      val scn = scenario(s"Personal Details ${users} Users")
          http("Personal Details API")
      scnList :+= scn
      users += 100

  // Setup
    scnList(): _*

You may want to try ChatGPT, I think it’s usable in your situation

I have already tried it but the code that it is generating is not working

Hi @sunil.karki,

I have already tried it but the code that it is generating is not working

What did you try and what is “not working”?
Is it specifically with gatling, or is it common code from scala and java (and a better question for one or the other forum)?


Hi @sbrevet,

I am able to convert above code to java, but, I want to add nothingFor (3) in case 100 and case 200 along with the rampUsers(5).during(1) & rampUsers(10).during(1) respectively. Could you please help me with that?

package neutronsApis;

import java.time.Duration;

import io.gatling.core.Predef.*;
import io.gatling.core.controller.inject.open.OpenInjectionBuilder;
import io.gatling.core.controller.inject.open.OpenInjectionProfile;
import io.gatling.http.Predef.*;
import io.gatling.javaapi.core.OpenInjectionStep;
import io.gatling.javaapi.core.PopulationBuilder;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.core.internal.OpenInjectionSteps;
import io.gatling.javaapi.core.internal.OpenInjectionSteps$;
import io.gatling.javaapi.http.HttpProtocolBuilder;
import scala.collection.immutable.ArraySeq;

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

public class PersonalDetailsConvertedCode extends Simulation {

    // Protocol
    HttpProtocolBuilder httpProtocol = http.baseUrl("https://morpheus.uat.creditsaison.xyz")
            .header("requestingSub", "5317f163-448d-48f2-8f30-900cdd669288")
            .authorizationHeader("Basic QGRNMU46UEAkJFcwckQ=");

    int users = 100;

    // Scenario
    private PopulationBuilder[] scnList() {
        PopulationBuilder[] scnList = new PopulationBuilder[3];

        for (int i = 0; i < 3; i++) {
            int currentUsers = users + (i * 100);
            OpenInjectionStep injectionProfile = null;
            switch (currentUsers) {
                case 100:
                    injectionProfile = rampUsers(5).during(1);
                case 200:
                    injectionProfile = rampUsers(10).during(1);;
                case 300:
                    injectionProfile =  rampUsers(15).during(1);

            PopulationBuilder scn = scenario("Personal Details " + currentUsers + " Users")
                    .exec(http("Personal Details API " + currentUsers + "Users")

            scnList[i] = scn;
        return scnList;

    // Setup

Hi @sunil.karki,

Sorry, but it doesn’t make sense in the topic.
Your working scala code doesn’t contain any nothingFor.

So, it isn’t the issue you mentioned.

If your issue is with chaining OpenInjectionSteps, this is definitely an another issue.

Do you know that injectOpen method can take either several OpenInjectionSteps (variadic) or a List<OpenInjectionStep>? Perhaps do you need the later as your injectionProfile variable.

Usually, we advise people to split the scenario definition (what a single user should do) from the injection profile.

Something like:

  // Scenario
  private static ScenarioBuilder scn = scenario("Personal Details")
      .exec(http("Personal Details API")

  // Setup

Does that make sense in your use case?


@sbrevet Thanks for the guidance !!
I’ll try using the List of OpenInjectionStep

And we need scenarios to be executed separately and same should be listed in the report for analysis that’s why we are using loop for generating scenarios.