Rest-Assured (API testing) Formatting in IDEA

Answered

Hey folks,

There's a library many of us use called Rest-Assured, that provides a slick framework for testing API endpoints from within your Java code. It's (probably) the primary Java-based library for that particular use case, but those of us that use IDEA for an IDE run into some interesting formatting issues.

Rest-Assured follows a "given-when-then" format to writing test cases.



But as someone who habitually autoformats my code (one of my favorite features in an IDE), similar code comes out a lot less pretty.
Screenshot at Dec 22 10-59-09.png

Messing around with formatter settings hasn't gotten me very far, and gratuitious use of formatter on/formatter off tags can slow us down by a fair bit.

Have any other IDEA users out there who use Rest-Assured found a good solution to this? The Rest-Assured community is stumped, and we'd love any help!

Thank you!

(Copy-pastable code below if you want to mess around)

given()
       .param("x", "y")
       .header("z", "p")
when()
       .get("something")
then()
       .statusCode(200)
       .body("x.y", equalTo("z");

9 comments
Comment actions Permalink

Hi!

I am also looking for some help here. But I think its not even possible - Rest Assured tests are just Java code in JUnit. How one could tell the difference?

0
Comment actions Permalink

+1 if intelliJ can sense this style. But for now, we can use //@formatter:off to turn off auto-formatting in the enclosed code:

 

//@formatter:off

given()
       .param("x", "y")
       .header("z", "p")
when()
       .get("something")
then()
       .statusCode(200)
       .body("x.y", equalTo("z");

//@formatter:on

0
Comment actions Permalink

I am not sure how IntelliJ can offer a smart formatting for that special case.

IntelliJ won't reformat the code below although it does not use any formatter instructions:

given()
.param("x", "y")
.header("z", "p").
when()
.get("something").
then()
.statusCode(200)
.body("x.y", equalTo("z"));

The leading dots are used inside blocks of given/expect/when/then. So it is a bit easier to see the structure.

It is not perfect, but I prefer this over formatter instructions in the code.

Now I have got a great idea! What if IntelliJ would be so smart to see this special use of trailing and ending dots in a call chain and format the code accordingly in a nice way to look like this:

given()
.param("x", "y")
.header("z", "p").
when()
.get("something").
then()
.statusCode(200)
.body("x.y", equalTo("z"));
0
Comment actions Permalink

I don't really like the idea with the trailing dots.

I think intellij is intelligent enough to realise you start a rest assured chain (when you start with io.restassured.RestAssured#given()), and should simply display everything intended except certain method calls in the chain (given, when, then).

Doesn't sound too complicated...

0
Comment actions Permalink

Actually, I don't even want the formatter to do this (because my colleagues using eclipse will destroy this for sure), I'd just like it to be displayed this way (as it's already happening, e.g., when you have a variable and IntelliJ just shows "val x = ..." instead of "List<String> x = ...".

0
Comment actions Permalink

I would like to mention that for me this is not a RestAssured-exclusive issue. What I'm actually looking for is a way to have meaningful indentation.

For example in code like

 

```

accountBuilder
    .title(MR)
    .name("Frank", "Underwood")
    .withMainAddress()
        .uniqueAddressId(1337)
        .streetAndNumber("Pennsylvania Ave","1600")
        .city("Washington, DC")
        .build()
    .birthDate(new IsoTime("1967-08-03T12:54:42+00:00"))
    .build()

```

 

I want the calls to the Address sub-builder to have one more level of indentation, because it signifies one more level of builder.

I'd be happy if there was an option for Java formatting, which adds indentation levels when there are too few (e.g. properly indenting { } blocks), but does not remove indentation if there is "too much" of it (as in, the dev added "too much" by design). It would be especially useful if such an option could apply only to lines with chained method calls (starting or ending in a dot).

0
Comment actions Permalink

You are welcome to submit a request for the code formatting feature at https://youtrack.jetbrains.com/issues/IDEA.

0
Comment actions Permalink

I do not think this is IntelliJ problem or that it should be solved at this level. There are obviously many other editors/autoformatters which will equally suffer from this.

This should be solved at the core - that is - RestAssured and other frameworks must transition to usage of proper nesting like given(...).when(...).then(...) with "..." possibly being a "new FooBarBuilder()......build()".

0

Please sign in to leave a comment.