0

According to this question I understand that pathParam is parameter for path in query. But I don't understand what is the usage of that instead of writing the full path in get query like this if we have only one path anyway?

given().
    baseUri("https://postman-echo.com")
           .pathParam("userId", "1")
.when()
          .get("/api/users/{userId}")
.then()
    .log().ifError()
           .assertThat()
           .statusCode(200)
nuzooo
  • 133
  • 1
  • 11
  • 1
    What about your `userID` is a value that comes from another API. You don't want to concate String in path. If only one pathParam in in literal value like your question, it provides no improvement. It will help the case 2+ pathParam and value of them come from somewhere else, not literal values. – lucas-nguyen-17 Dec 09 '22 at 13:54

1 Answers1

1

You could have your spec preserved and reuse parameter in different apis like:

public static void main(String[] args) {
    RequestSpecification spec = given().
            baseUri("https://httpbin.org/anything")
            .pathParam("param", "123");

    spec
            .get("/api1/{param}")
            .then()
            .log().body();

    spec
            .get("/api2/{param}")
            .then()
            .log().body();
}
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • So why not to do baseUri("https://httpbin.org/anything/123") I mean what is the advantage of using this over the path itself? – nuzooo Dec 09 '22 at 13:10
  • 1
    Are you asking about what is advantage of hardcoding a value instead of making it flexible? The advantage is that you have less lines of code. The disadvantages is that you cannot control it in runtime. – Alexey R. Dec 09 '22 at 13:50
  • So in case I have only one request the better option is to use the full path in get method, if I have to concate 2 requests like your example there it will be more efficient ? – nuzooo Dec 11 '22 at 11:23
  • 1
    If you always have the same URL it does not make sense to make use of `pathParam`. You can just hardcode it in `basUri` or `get`. If you are going to control the parameter value during your test lifetime or re-use the parameter value, you will go with `pathParam`. – Alexey R. Dec 12 '22 at 14:24
  • But I see actualy in your eaxmple you use in the same param in two requests, that is ok and in such case you can use the param in two different request but there is no option to use in two different params in two different requests in same test, isn't it? – nuzooo Dec 12 '22 at 18:33