learningIsFun
learningIsFun

Reputation: 152

Looking to pass dynamic data as jsonpath request

 @Test
public void basicAuthLogin() {
    //language=JSON
    String jsonBody = "{\n" +
            "  \"name\": \"Foo\"\n" +
            "}";

    given().auth().preemptive().basic(username, password)
            .body(jsonBody)
            .contentType(ContentType.JSON)
            .when()
            .post("http://localhost:8080/secured/hello")
            .then()
            .statusCode(200);
}

I wanted to pass dynamic data for name instead of "Foo". How can I do that?

Upvotes: 1

Views: 298

Answers (1)

Dhiral Kaniya
Dhiral Kaniya

Reputation: 2051

You need to use DataProvider here. Which actually provides data to your test case in the runtime. There are multiple ways to provide dynamic data to the test case before, after, and during the test case.

  • Use @RunWith(Parameterized.class) in case if you are using JUnit and you can provide data with @Parameters

  • Use @DataProvider (name = “name_of_dataprovider”) and create data-provider method in case if you are using TestNG and you can add property @Test (dataProvider = "data-provider") in your testCase.

  • You can provide input from a file, you can write generic mapper with the help of objectMapper or Jackson lib fetch data and convert into the object at runtime and use it as an input parameter.

Upvotes: 1

Related Questions