-1

first of all i have method like this ;

@When("^I send google-login-api with \"([^\"]*)\" token$")
    public void googleLoginApi(String googleTokenSuccess) throws IOException {

        String body = new String(Files.readAllBytes(Paths.get("src/test/resources/config/environments/googleLogin.json")));
        JSONObject jsonObject = new JSONObject(body);

        String googleToken = token.getString(googleTokenSuccess);
        jsonObject.put("token",googleToken);



        response = RestAssured.given()
                .baseUri(prp_url)
                .accept("application/json")
                .contentType("application/json")
                .queryParam("rememberMe","true")
                .body(jsonObject.toString())
                .when()
                .post("/auth/google")
                .then().assertThat().extract().response();
    }

And i call this method in gherkin style ;

  Scenario: 3AF GoogleLoginSuccess
    When I send google-login-api with "google token success" token
    Then response status code should be 200
    Then response body "deviceInfo.name" is in "Firefox, Windows 10"

When I call token with different names in the BDD structure(When I send google-login-api with "google token success" token), I want the method parameter name to change as well. For example when i call xyz token with BDD style, method parameter String googleTokenSuccess paramater is equal to xyz.

  • so you want `When I send xyz with "google token success" token` to to call `public void xyz(String something)`? Or what's the method (parameter?) name that should change? – zapl Sep 19 '22 at 13:16
  • No actually `When I send google-login-api with "xyz" token` and my method paramater name will change like `public void googleLoginApi(String xyz)` – Mert Çevik Sep 19 '22 at 13:19
  • How would a method parameter name change or matter in source code? You can't have 2 method signatures with the same type. The signature you have is `void googleLoginApi(String)` – zapl Sep 19 '22 at 13:21
  • My main goal is; I have a token.json file and inside the file I have lots of tokens. So when i call xyz token with gherkin style xyz token should be come. When i call abc token with gherkin style abc token should be come – Mert Çevik Sep 19 '22 at 13:23
  • Ok, that means the value of the `googleTokenSuccess` parameter needs to change, which looks like it should already work assuming that calling it with `"google token success"` works and gives you the "google token success" value from your `googleLogin.json`, e.g. if it's `{"google token success":"abc"}`, the value of `googleToken` should be `"abc"`. When calling it with `"xyz"` and you have e.g. `{"xyz": "cde"}` in the json, even if the variable name is not fitting it would be `cde` – zapl Sep 19 '22 at 13:32
  • Exactly. Because i want to my different steps uses 1 method. For example `When I send google-login-api with "google token success"` gives `google token success` from .json file and `When I send google-login-api with "google sign up required"` token gives `google sign up required token` from .json file – Mert Çevik Sep 19 '22 at 13:36
  • But is it not working already? I don't see anything wrong with that code besides variable names that are maybe not fitting their purpose but they don't matter in the end when running the code. And you can edit the source code of `googleLoginApi` if you don't like them – zapl Sep 19 '22 at 13:42
  • Btw what is `token`? It's not defined within that method and when calling `token.getString(googleTokenSuccess)` it's not loading from the json file but somewhere else. Maybe you need to do `jsonObject.getString(..` instead? And shouldn't there be a new empty JSONObject used for `.body(jsonObject.toString())`, like https://gist.github.com/zapl/def4777cdfe732f48be593c52c6d64f4 ? – zapl Sep 19 '22 at 13:56

1 Answers1

0

I'm no expert here, but according to the docs, you probably want to do something like:

@When("I send google-login-api with {string} token")
public void googleLoginApi(String googleTokenSuccess) throws IOException {
   // Here, googleTokenSuccess should be set to the value from the scenario
}
Pateman
  • 2,727
  • 3
  • 28
  • 43