admin管理员组

文章数量:1297062

I need to modify the code below so the check() will pass, even if values are not exact as long as abs(val1 -val2) < 1.00

.check(
  jsonPath("$.data.attributes.contentValue")
    .is(session -> session.getString("contentValue"))
)

I've tried using the session variable in multiple ways but it seems unavailable inside the validate() method. Also even the replaceAll() method is unavailable there. The code below fails to compile with Cannot resolve method 'replaceAll(String, String)' and Cannot resolve method 'get(String)'

.check(
  jsonPath("$.data.attributes.contentValue")
    .transform(String::valueOf)
    .validate((actualValue, session) -> {
      double actualDouble = Double.parseDouble(actualValue.replaceAll("$", "").replace(",", ""));
      double expectedDouble = (double) session.get("expectedDoubleValue");
      return Math.abs(expectedDouble - actualDouble) <= 1.00;
    }
  )
)

I need to modify the code below so the check() will pass, even if values are not exact as long as abs(val1 -val2) < 1.00

.check(
  jsonPath("$.data.attributes.contentValue")
    .is(session -> session.getString("contentValue"))
)

I've tried using the session variable in multiple ways but it seems unavailable inside the validate() method. Also even the replaceAll() method is unavailable there. The code below fails to compile with Cannot resolve method 'replaceAll(String, String)' and Cannot resolve method 'get(String)'

.check(
  jsonPath("$.data.attributes.contentValue")
    .transform(String::valueOf)
    .validate((actualValue, session) -> {
      double actualDouble = Double.parseDouble(actualValue.replaceAll("$", "").replace(",", ""));
      double expectedDouble = (double) session.get("expectedDoubleValue");
      return Math.abs(expectedDouble - actualDouble) <= 1.00;
    }
  )
)
Share edited Feb 12 at 10:08 Stéphane LANDELLE 6,6232 gold badges12 silver badges16 bronze badges asked Feb 11 at 16:36 DTMDTM 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is really a straightforward application of the documentation: https://docs.gatling.io/reference/script/core/checks/#validate

jsonPath("$.data.attributes.contentValue")
  .ofDouble()
  .validate(
    "is +/- 1.0",
    (contentValue, session) -> {
      double expectedDoubleValue = session.getDouble("expectedDoubleValue");
      if (Math.abs(contentValue - expectedDoubleValue) > 0.1) {
        throw new RuntimeException("Wrong value");
      }
      return contentValue;
    }
  )

本文标签: