Alexb1999
Alexb1999

Reputation: 109

Passing session to function in Gatling Java

I am trying to pass the session to a function in Gatling as shown in .body():


                    exec(http("ED/CTS/SGM")
                                .post(IDS_BASE_URL + edEndpoint)
                                .body(StringBody(createBodyStringForStudentAssignmentSetup(session)))
                                .headers(getHeaderMap)
                                .check(status().is(201),
                                        jsonPath("$.assignments[0].refId").saveAs("assignmentRefId")));


The function being called is shown here:


public class CreateAssignmentDataSetup {

    public static Body.WithString createBodyStringForStudentAssignmentSetup(Session session)
    {

        
        String title;

        String assignmentName = "PerformanceTest_AssignmentApi_";

        title = assignmentName.concat(LocalDate.now().toString());
        
        String studentsList = session.getString("listOfStudents");


.... 
....

       return StringBody(body);
 
    }
 

Is it possible to pass "session" values like this to "createBodyStringForStudentAssignmetnSetup()" like this, since using an exec(session-> block (shown below) will not allow requests to be triggered for some reason:

exec(session -> {

});

Thanks

Upvotes: 0

Views: 559

Answers (1)

Stéphane LANDELLE
Stéphane LANDELLE

Reputation: 6623

StringBody takes a Function<Session, String> so you want to write:

    public static String createBodyStringForStudentAssignmentSetup(Session session)
    {

        
        String title;

        String assignmentName = "PerformanceTest_AssignmentApi_";

        title = assignmentName.concat(LocalDate.now().toString());
        
        String studentsList = session.getString("listOfStudents");


.... 
....

       return body;
    }

       
.body(StringBody(CreateAssignmentDataSetup::createBodyStringForStudentAssignmentSetup))

Upvotes: 1

Related Questions