9

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?

2 Answers 2

5

A solution I found so far it to turn off formatting for certain sections, so that IDE does not destroy that.

In Intellij

  1. Surround the code fragment with

    // @formatter:off
    ...
    // @formatter:on
    
  2. Preferences -> Code Style -> General -> Formatter Control -> Enable formatter markers in comments

enter image description here

This does not auto-format RestAssured section, but at least does not destroy it.

5
  • Is there any other way of doing this? Maybe modifying the ide's schema?
    – Dan
    Sep 28, 2015 at 16:35
  • @Dan What is IDE's schema?
    – dzieciou
    Sep 29, 2015 at 14:32
  • Sorry, I meant the "scheme." From the screenshot you provided you can import a code style scheme. Is it possible to create a scheme that will skip the autoformatting?
    – Dan
    Sep 29, 2015 at 17:41
  • @Dan I haven't found such a schema.
    – dzieciou
    Jan 1, 2021 at 12:14
  • Editor -> Code Style -> Formatter tab -> Do not format: [<test files pattern>]
    – Marboni
    Jan 11 at 5:21
0

I didn't like disable formatting approach, but I did something like this, not awesome but little bit readable (added empty line)

given()
        .param("x", "y")
        .header("z", "p")

        .when()
        .get("something")

        .then()
        .statusCode(200);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.