Auto-formatting RestAssured test cases in my IDE

1,713 views
Skip to first unread message

Maciej Gawinecki

unread,
Sep 29, 2014, 4:58:49 AM9/29/14
to rest-a...@googlegroups.com
Hi,

We have our tests implemented in RestAssured, using nice and readable
convention of indentation for given/when/then parts.

given().
  param("key1", "value1").
  param("key2", "value2").
when().
  post("/somewhere").
then().
  body(containsString("OK"));


However, when I apply automatic formatting with template (Google code
style for Java) in Intellij IDEA, the formatting I had gets destroyed.

given().
        param("key1", "value1").
        param("key2", "value2").
        when().
        post("/somewhere").
        then().
        body(containsString("OK"));


Is there a way my IDE or maven formats RestAssured statements with
expected indentations, while the rest keeps with respect to the Google
template? Or at least, it doesn't destroy what I have set for
RestAssured statements?

What I found so far is to disable formatting for certain sections:

1. Surround the code fragment with

     // @formatter:off
     ...
     // @formatter:on


2. Preferences -> Code Style -> General -> Formatter Control -> Enable
formatter markers in comments

Thanks,
Maciej

Johan Haleby

unread,
Sep 30, 2014, 1:52:18 AM9/30/14
to rest-a...@googlegroups.com
I haven't found a good solution for it. I mainly use @formatter:off / on if necessary. Would be nice to come up with a better solution. 

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

seamus....@retroficiency.com

unread,
Dec 22, 2015, 12:54:19 AM12/22/15
to REST assured
Just getting spun up with Rest-Assured and wanted to second the original poster's question, since it'd been awhile.

How do folks handle formatting their Rest-Assured code? (And making sure that format stays consistent across a team)

Johan Haleby

unread,
Dec 22, 2015, 12:56:45 AM12/22/15
to rest-a...@googlegroups.com
I don't have any updates on this unfortunately. Personally I either just don't format or use @formatter:off. Would be interesting if you could ask the same question to JetBrains?

Seamus Reynolds

unread,
Dec 22, 2015, 10:45:29 AM12/22/15
to rest-a...@googlegroups.com
That's a good idea, I'll see if I can't find a good way to ask this of the community and link back to it here. Have folks had any luck doing this with any other IDEs?

--
You received this message because you are subscribed to a topic in the Google Groups "REST assured" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rest-assured/e6Sx_4way2M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rest-assured...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Seamus Reynolds
Retroficiency

80Vikram

unread,
Aug 19, 2016, 11:44:07 AM8/19/16
to REST assured
Hi All,

Anybody found a good solution for this , I've exactly same problem to solve as Maciej had mentioned couple of years back.

Thanks in advance.

Regards,
Vikram

Paul Hoadrea

unread,
Sep 23, 2016, 10:06:23 AM9/23/16
to REST assured
I have the same problem with RestAssured and IntelliJ. The only solution that I found is not to use the formatter :(

KS

unread,
Nov 10, 2016, 2:24:58 AM11/10/16
to REST assured
Sorry, there was a mistake in my previous post (you have to use expect() instead of then() with this approach):

private final RequestSpecification scenario = given();

@Test
public void test() throws IOException {
scenario.given()
.param("name", "John")
.param("age", 21);

scenario.expect()
.statusCode(201);

scenario.when()
.post("/register");

}

KS

unread,
Nov 10, 2016, 4:59:20 AM11/10/16
to REST assured
Another possible workaround:

private final RequestSpecification request = given();
private ValidatableResponse response;

@Test
public void test() {
GIVEN(s -> s

.param("name", "John")
.param("age", 21)
    );

WHEN(s -> s.post("register"));

THEN(r -> r.statusCode(201));
}

private void GIVEN(Consumer<RequestSpecification> spec) {
spec.accept(request);
}

private void WHEN(Function<RequestSpecification, Response> spec) {
response = spec.apply(request).then();
}

private void THEN(Consumer<ValidatableResponse> spec) {
spec.accept(response);
}
Reply all
Reply to author
Forward
0 new messages