# I need help in converting a scala code to java

**URL:** <https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849>\
**Category:** Gatling (Open-Source)\
**Created:** [April 2, 2024, 1:06am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849 "2024-04-02T01:06:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunil.karki](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sunil.karki](https://community.gatling.io/u/sunil.karki)\
**Post date:** [April 2, 2024, 1:06am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/1 "2024-04-02T01:06:06Z")

</div>

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.

```auto
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")
    .contentTypeHeader("application/json")
    .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")
        .exec(
          http("Personal Details API")
            .post("/api/v1/appForm")
            .check(status.is(424))
            .body(RawFileBody("./src/test/resources/requestBody/personaldetails.json")).asJson
        )
        .inject(injectionProfile)
      
      scnList :+= scn
      users += 100
    }
    scnList
  }

  // Setup
  setUp(
    scnList(): _*
  ).protocols(httpProtocol)
}

```

---

<div class="post-metadata">

**Author:** ![trinp](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/trinp/32/1593_2.png) [@trinp](https://community.gatling.io/u/trinp)\
**Post date:** [April 2, 2024, 4:06am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/2 "2024-04-02T04:06:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sunil.karki](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sunil.karki](https://community.gatling.io/u/sunil.karki)\
**Post date:** [April 2, 2024, 4:58am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/3 "2024-04-02T04:58:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [April 2, 2024, 6:36am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/4 "2024-04-02T06:36:09Z")

</div>

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)?

Cheers!

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [April 3, 2024, 7:42am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/6 "2024-04-03T07:42:16Z")

</div>

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 `OpenInjectionStep`s, this is definitely an another issue.

Do you know that `injectOpen` method can take either several `OpenInjectionStep`s (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:

```java
  // Scenario
  private static ScenarioBuilder scn = scenario("Personal Details")
      .exec(http("Personal Details API")
          .post("/api/v1/appForm")
          .check(status().is(503))
          .body(RawFileBody("./requestBody/personaldetails.json")).asJson());

  // Setup
  {
    setUp(scn.injectOpen(
        rampUsers(5).during(1),
        rampUsers(10).during(1),
        rampUsers(15).during(1)
    )).protocols(httpProtocol);
  }

```

Does that make sense in your use case?

Cheers!

---

<div class="post-metadata">

**Author:** ![sunil.karki](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sunil.karki](https://community.gatling.io/u/sunil.karki)\
**Post date:** [April 3, 2024, 8:59am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/7 "2024-04-03T08:59:24Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [May 3, 2024, 9:00am UTC](https://community.gatling.io/t/i-need-help-in-converting-a-scala-code-to-java/8849/8 "2024-05-03T09:00:01Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
