Search code examples
serenity-bddcucumber-serenityserenity-platform

Serenity-BDD screenplay REST interactions with a request specification


I want to send a post request in my test using the format:

RequestSpecification requestSpecification = RestAssured.given()
                                                            .contentType(ContentType.JSON)
                                                            .body(taskDetailsFormSubmission);

actor.attemptsTo(
    Post.to(path).with(requestSpecification)
);

I am creating a RequestSpecification and parsing a JSON file in one of my directories.

The problem however is that with() only accepts the type RestQueryFunction. Looking at the interface:

public interface RestQueryFunction extends Function<RequestSpecification,RequestSpecification> {}

I'm just not really sure how to apply the RequestSpecification to the POST request.


Solution

  • Function<RequestSpecification,RequestSpecification> is a case of Function<T,R> java functional interface.

    The method with() requires RestQueryFunction means you have to provide an implementation of RestQueryFunction which might be a class, anonymous class or lambda expression. It follows the rule:

    • Parameter is RequestSpecification.
    • Return type is RequestSpecification.

    You could try

    actor.attemptsTo(
        Post.to(path).with(r -> r.contentType(ContentType.JSON) 
                               .body(taskDetailsFormSubmission);
        )
    );