<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marina Galenko on Medium]]></title>
        <description><![CDATA[Stories by Marina Galenko on Medium]]></description>
        <link>https://medium.com/@marina.galenko?source=rss-30f5215c4f17------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*F64dNoNgZEsYPasj</url>
            <title>Stories by Marina Galenko on Medium</title>
            <link>https://medium.com/@marina.galenko?source=rss-30f5215c4f17------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 18:26:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marina.galenko/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Best practices for REST Assured]]></title>
            <link>https://medium.com/@marina.galenko/best-practices-for-rest-assured-5ff5009eb55a?source=rss-30f5215c4f17------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ff5009eb55a</guid>
            <category><![CDATA[qa]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[restassured]]></category>
            <category><![CDATA[qa-automation]]></category>
            <dc:creator><![CDATA[Marina Galenko]]></dc:creator>
            <pubDate>Wed, 09 Feb 2022 11:17:20 GMT</pubDate>
            <atom:updated>2022-02-09T11:20:24.190Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi there!</p><p>Here’s some tips on using REST Assured that I gathered during my first API testing project.</p><p>Initially I wrote it as part of a local knowledge base for my colleagues, but now decided to translate it to English in case someone else finds it useful.</p><p>Please note that I’m not in any way claiming that I invented all this stuff by myself. Mostly this is something I discovered during research for my own project.</p><p>Also I’m but a mere QA baby with half a year of working experience, so please be gentle.</p><p>Here’s a link to GitHub repo, you can find the same text and a tiny project utilizing these tips there, so fork it all you please:<br><a href="https://github.com/merridewberry/rest-assured-best-practices">https://github.com/merridewberry/rest-assured-best-practices</a></p><h3>Use specifications</h3><p>Constructions such as</p><pre>given()<br>.accept(ContentType.JSON)</pre><p>are used in almost every request. Even if it’s only two lines, you still can cut them in half by calling a single method — and make your code more readable by doing so. For that you can either use a separate method:</p><pre>public static RequestSpecification requestSpec() {<br>    return given()<br>        .accept(ContentType.JSON);<br>}</pre><p>or builder:</p><pre>RequestSpecification requestSpec = new RequestSpecBuilder()<br>    .setAccept(ContentType.JSON)<br>    .build();</pre><p>And just call this method or use this builder in every request.</p><p>Though it’s highly unlikely that these two lines would be the only code you could move to specifications. You can add there some authorization headers, set host and port, set up logging, add Allure filters, and do whatever else you wish to do in every request.</p><p>This will make your code much easier to understand, because the person who reads it most likely doesn’t really want to be reminded that you authenticated and set content type every single time. For example, this is the request specification I used in my recent project:</p><pre>public static RequestSpecification requestSpec() {<br>    return given()<br>            .filter(new AllureRestAssured())<br>            .header(&quot;User-Agent&quot;, USER_AGENT)<br>            .contentType(ContentType.JSON)<br>            .accept(ContentType.JSON)<br>            .auth().oauth2(getProperty(&quot;oauth-token&quot;))<br>}</pre><p>You do the same thing with response too. Your options are a bit more limited in this case, but you still can check response code and time, and whether receiver response contains some specific headers and fields. Just like with request specification, you could use either method:</p><pre>public static ValidatableResponse responseSpecOk(Response response) {<br>    return response<br>        .then()<br>        .statusCode(200)<br>        .time(lessThanOrEqualTo(responseTime));<br>}</pre><p>or builder:</p><pre>ResponseSpecification responseSpecOk = new ResponseSpecBuilder()<br>        .expectStatusCode(200)<br>        .expectResponseTime(lessThanOrEqualTo(responseTime))<br>        .build();</pre><h3>Store your endpoints in a separate place</h3><p>You shouldn’t add endpoints directly into the method where you want to use them. Even when there are not many of them or if this specific endpoint is only used in your code once, if you’ll ever need to change it, something is quite likely to get lost, and in the end you’ll spend more time on finding the cause of your problem than on setting you endpoints nice and tidy to begin with.</p><p>You can either create a separate class to manage your endpoints or just store them in some properties file. Something like this:</p><pre>public class Endpoints {<br>    public static String booking = &quot;booking&quot;;<br>    public static String bookingId = &quot;booking/{id}&quot;;<br>    public static String auth = &quot;auth&quot;;<br>}</pre><p>If your endpoint has some changeable part (an ID for example), you can store it this way:</p><pre>String endpoint = &quot;endpoint/{id}&quot;</pre><p>In this case you’ll just need to pass the value you want to use as an argument:</p><pre>.get(endpoint, id)</pre><h3>Store your base URI in a separate place</h3><p>Just like headers, some part of the URL is present in every one of your requests — so you can as well store it separately and initiate it just once.</p><p>You can either put it inside some class as a variable or use a properties file — I prefer the latter.</p><p>There are two ways of initializing base URI:</p><pre>given()<br>    .baseUri(getProperty(&quot;baseUri&quot;))</pre><p>and:</p><pre>RestAssured.baseURI = getProperty(&quot;baseUri&quot;);</pre><p>And speaking of calling the base URI, you also have two options.</p><p>First — to call it in every request. This way you’ll have more code, but if you have to use few different base URIs it’ll have to do.</p><p>Second — to call it once, for example, inside the request specification.</p><h3>Use REST Assured built-in functional for converting objects</h3><p>There is no need to convert POJO into Json using some external libraries because REST Assured is perfectly capable of doing it by itself. You’ll just need to add you POJO into the request body:</p><pre>Pojo pojo = new Pojo();<br><br>given()<br>    .body(pojo)<br>    .post(URI);</pre><p>It can convert Json into POJO as well:</p><pre>Pojo pojo = given()<br>    .get(URI)<br>    .then()<br>    .extract().body().as(Pojo.class)</pre><p>And there’s more! REST Assured can convert Hash Maps into Json as well — even nested ones:</p><pre>Map &lt;Object, Object&gt; outerMap = new HashMap&lt;Object, Object&gt;();<br>Map &lt;Object, Object&gt; innerMap = new HashMap&lt;Object, Object&gt;();<br><br>innerMap.put(&quot;key&quot;, &quot;value&quot;);<br><br>outerMap.put(&quot;key&quot;, &quot;value&quot;);<br>outerMap.put(&quot;innerMap&quot;, innerMap);<br><br>given()<br>    .body(outerMap)<br>    .post(URI);</pre><h3>Use Json schema validation</h3><p>Json schema validation is a simple way to make sure that the data you’ve received has all the expected fields and variables are of correct types.</p><p>For example, for Json like this:</p><pre>{<br>    &quot;id&quot;: 1,<br>    &quot;item&quot;: {<br>        &quot;key1&quot;: &quot;integer or sting&quot;,<br>        &quot;key2&quot;: &quot;string&quot;,<br>        &quot;key3&quot;: &quot;not required&quot;<br>    }<br>}</pre><p>Json schema will look like this:</p><pre>{<br>    &quot;type&quot;: &quot;object&quot;,<br>    &quot;properties&quot;: {<br>        &quot;id&quot;: {<br>            &quot;type&quot;: &quot;integer&quot;<br>        },<br>        &quot;item&quot;: {<br>            &quot;type&quot;: &quot;object&quot;,<br>            &quot;properties&quot;: {<br>                &quot;key1&quot;: {<br>                    &quot;type&quot;: [&quot;string&quot;, &quot;integer&quot;]<br>                },<br>                &quot;key2&quot;: {<br>                    &quot;type&quot;: &quot;string&quot;<br>                },<br>                &quot;key3&quot;: {<br>                    &quot;type&quot;: &quot;string&quot;<br>                }<br>            },<br>            &quot;required&quot;: [&quot;key1&quot;, &quot;key2&quot;]<br>        }<br>    },<br>    &quot;required&quot;: [&quot;id&quot;, &quot;item&quot;]<br>}</pre><p>This schema makes sure that received Json has all the fields that were marked as required and all the values in the fields has the required type.</p><p>You can write Json schemas by yourself, though they are a bit tricky and not very human-friendly, so I’d strongly recommend you to use one of the many online tools for creating schemas and not waste your time. For example, I use this one: <a href="https://www.liquid-technologies.com/online-json-to-schema-converter">https://www.liquid-technologies.com/online-json-to-schema-converter</a></p><p>To use Json schema validation you’ll need a json-schema-validator library (I use the one from io.rest-assured) and the following code:</p><pre>.body(matchesJsonSchemaInClasspath(&quot;schema.json&quot;));</pre><p>You can find more information about Json schema here: <a href="https://json-schema.org/">https://json-schema.org/</a></p><h3>Move repeatedly performed actions to a separate method</h3><p>There’s no need to check whether the response is valid ten times for ten tests. You may as well just make one separate method and call it every time you need it.</p><p>This applies to any type of frequently performed actions. It’s much easier to read and maintain the code where all the constructors, specifications and checks exist in separate blocks with which you can build your tests.</p><p>Like this for example:</p><pre>public static RequestSpecification requestSpec = new RequestSpecBuilder()<br>    .setBaseUri(BASE_URI)<br>    .setContentType(ContentType.JSON)<br>    .setAccept(ContentType.JSON)<br>    .build();<br><br>public static ResponseSpecification responseSpec(int statusCode) {<br>    return new ResponseSpecBuilder()<br>            .expectStatusCode(statusCode)<br>            .expectResponseTime(lessThanOrEqualTo(responseTime))<br>            .build();<br>}<br><br>public static Response createItem(Pojo body) {<br>    return given()<br>        .spec(requestSpec)<br>        .body(body)<br>        .post(Uri);<br>    }<br><br>public static ValidatableResponse createItem_valid(Response response) {<br>    return response<br>            .then()<br>            .spec(responseSpec(200))<br>            .body(matchesJsonSchemaInClasspath(&quot;schemas/CreateItem.json&quot;));<br>}<br><br>@Test<br>public void createItemTest() {<br>    createItem_valid(<br>        createItem(body))<br>}</pre><h3>Use Hamcrest Matchers</h3><p>When there’s no real need to deserialize response into POJO, matchers from the Hamcrest library are the fastest and easiest way to validate some specific values in the response.</p><p>They allow you to check whether the response contains some value at all, or if there is some value in specific field, or whether the array contains the items you need.</p><pre>.body(&quot;id&quot;, equals(getId()))<br>.body(&quot;id&quot;, greaterThanOrEqualTo(1))<br>.body(&quot;items&quot;, either(hasItem(&quot;a&quot;)).and(hasItem(&quot;b&quot;))</pre><p>Of course there are other ways around it, but I find using Hamcrest matchers the most convenient one.</p><p>You can find full Hamcrest Matcher documentation here: <a href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html">http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html</a></p><h3>Use preexisting REST Assured methods to add headers</h3><p>There is a shorter and neater way to add headers for content type, accepted type, authorization and much more:</p><pre>given()<br>   .contentType(ContentType.JSON)<br>   .accept(ContentType.JSON)</pre><p>Apart from being shorter, these methods allow you to use preexisting enums, and this way no typos may occur in your code.</p><p>Though note that, for example, using enum <em>ContentType.JSON</em> in the <em>accept()</em> method results in sending not only <em>application/json</em> content type, but also <em>application/javascript</em>, <em>text/javascript</em> and <em>text/json</em>. This may cause some issues in certain APIs, so look out for that, maybe you’ll need to set content type using sting.</p><h3>Check response status without assertThat()</h3><p>There’s no need to type</p><pre>.assertThat(statusCode(200))</pre><p>the shorter option does the job just fine:</p><pre>.statusCode(200)</pre><p>Pass status codes as arguments</p><p>If you have quite a few negative tests with different expected status codes, you may end up with the bunch of similar methods to check them. Instead of that you can just pass expected status code as an argument and use just one method for all tests:</p><pre>public static ResponseSpecification responseSpecError(int statusCode) {<br>    return new ResponseSpecBuilder()<br>        .expectStatusCode(statusCode)<br>        .expectResponseTime(lessThanOrEqualTo(responseTime))<br>        .build();<br>}</pre><h3>Use logging for debugging tests</h3><p><em>log()</em> method allows you to get information about both sent request and received response. To receive request log, you’ll need to place it before the method that you use to send a request (<em>get()</em>, <em>post()</em>, etc.), to receive response log — after it.</p><p>There are the following methods available:</p><pre>.log().all()<br>.log().params()<br>.log().body()<br>.log().headers()<br>.log().cookies()</pre><p>Though you shouldn’t leave these logging methods in your tests after you’ve got them up and running. The amount of data you receive may be so big that all your test results won’t fit inside IDE console, and even if they’ll do, it will be quite hard to read.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ff5009eb55a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using POJO in REST Assured]]></title>
            <link>https://medium.com/@marina.galenko/using-pojo-in-rest-assured-fd15b620cdda?source=rss-30f5215c4f17------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd15b620cdda</guid>
            <category><![CDATA[qa]]></category>
            <category><![CDATA[pojo]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[restassured]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Marina Galenko]]></dc:creator>
            <pubDate>Fri, 04 Feb 2022 17:25:44 GMT</pubDate>
            <atom:updated>2022-02-04T17:25:44.546Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi there!</p><p>Sometime ago my team leader tasked me with writing a little guide about using POJO in REST Assured for my colleagues. I thought it may be helpful for someone else, so I might as well translate it in English. And, here it is!</p><p>Here’s a link to the GitHub repo, it contains the same guide and a tiny Java project: <a href="https://github.com/merridewberry/pojo">https://github.com/merridewberry/pojo</a></p><p>Please note that I’m still a junior QA engineer, so if I don’t know something… Well, I’ll learn someday?</p><h3>What is POJO</h3><p>POJO stands for Plain Old Java Object, it’s a simple Java object not restricted by any framework limitations (except for basic Java specifications of course) and viable in any environment.</p><p>POJOs offer for direct and flexible approach to data serialization and deserialization, so their main role in testing with RestAssured is to process JSONs both in the request and response bodies.</p><p>They allow you to set up a request JSON containing just about any data you may need. For example, you can use a single POJO class for multiple similar JSONs with slightly different set of fields, so you can avoid repeating the same code all over again. Or you can create multi-level nested JSONs keeping their structure simple and easy to understand.</p><p>When working with response bodies, POJOs also allow you to extract any value you may need and access nested data — and all that, as already stated above, in an easy-to-understand way and without repeating the same code multiple times.</p><h3>How to create a POJO and what to do with it afterwards</h3><p>POJO is a public class with private variables, and to access or modify them you have to use setter and getter methods. POJO also needs at least a default constructor to specify the set of arguments for any instance of this POJO. So in the most basic way a POJO will look like this:</p><pre>public class Book {<br><br>    private String title;<br>    private String author;<br><br>    public Book() {<br> }<br><br> public String getTitle() {<br>  return title;<br> }<br><br>    public void setTitle(String title) {<br>  this.title = title;<br> }<br><br>    public String getAuthor() {<br>  return author;<br> }<br><br>    public void setAuthor(String author) {<br>  this.author = author;<br> }<br>}</pre><p>As with any other Java method, you can overload constructors. This way you can make a few sets of arguments you may need in your POJO instances. For example these constructors allow you to create just a book without an author and a title or a book with both an author and a title, but not anything in-between:</p><pre>public Book() {<br>}<br><br>public Book(String title, String author) {<br>   this.title = title;<br>   this.author = author;<br>}</pre><p>Though nothing stops you from creating a book with default empty constructor and then adding just one argument to it using setter method.</p><p>You can create an instance of your POJO in a following manner:</p><pre>Book book1 = new Book();</pre><p>or:</p><pre>Book book2 = new Book(&quot;War and Peace&quot;, &quot;Leo Tolstoy&quot;);</pre><h3>Putting POJO into practice</h3><p>To demonstrate how POJOs work I will use Restful Booker API, you can find the documentation here: <a href="https://restful-booker.herokuapp.com/apidoc/index.html">https://restful-booker.herokuapp.com/apidoc/index.html</a></p><p>Both request and response bodies in this API utilize JSON with the similar structure:</p><pre>{<br>    &quot;firstname&quot; : &quot;Jim&quot;,<br>    &quot;lastname&quot; : &quot;Brown&quot;,<br>    &quot;totalprice&quot; : 111,<br>    &quot;depositpaid&quot; : true,<br>    &quot;bookingdates&quot; : {<br>        &quot;checkin&quot; : &quot;2018-01-01&quot;,<br>        &quot;checkout&quot; : &quot;2019-01-01&quot;<br>    },<br>    &quot;additionalneeds&quot; : &quot;Breakfast&quot;<br>}</pre><p>To create POJO for this JSON we have to create new Booking class where we will initialize all the variables we need the same way we did it in the previous example.</p><p>You may notice that the <em>bookingdates </em>field contains two nested values, and we don’t have the correct data type right away. This problem is quite easy to solve though — all you need to do is to create the <em>BookingDates </em>class:</p><pre>public class BookingDates {<br><br>    private String checkin;<br>    private String checkout;<br><br>    public BookingDates(String checkin, String checkout) {<br>        this.checkin = checkin;<br>        this.checkout = checkout;<br>    }<br>}</pre><p>Now there is the data type we can use for the <em>bookingdates </em>variable and with that we have everything we need to create our <em>Booking </em>class:</p><pre>public class Booking {<br><br>    private String firstname;<br>    private String lastname;<br>    private Integer totalprice;<br>    private Boolean depositpaid;<br>    private BookingDates bookingdates;<br>    private String additionalneeds;<br><br>    public Booking(String firstname, String lastname, Integer totalprice, Boolean depositpaid, BookingDates bookingdates, String additionalneeds) {<br>        this.firstname = firstname;<br>        this.lastname = lastname;<br>        this.totalprice = totalprice;<br>        this.depositpaid = depositpaid;<br>        this.bookingdates = bookingdates;<br>        this.additionalneeds = additionalneeds;<br>    }<br>}</pre><p>You can use the same principle to create POJOs for JSON with multiple levels of nesting.</p><p>Since all the variables in both classes are private, and we will need to access them, we should write each of them a getter method:</p><pre>public String getFirstname() {<br>return firstname;<br>}<br><br>public String getLastname() {<br>    return lastname;<br>}<br><br>(and so on)</pre><p>This concludes all the preparation our POJO will need, so we can proceed to serialization and deserialization.</p><h3>Serialization</h3><p>As an example of serialization with POJO we’ll make a JSON for creating a new booking.</p><p>To do that we’ll have to create instances of the <em>Booking </em>and <em>BookingDates</em> classes with all the required data:</p><pre>BookingDates bookingDates = new BookingDates(&quot;2021-08-31&quot;, &quot;2021-09-10&quot;);  <br>Booking booking = new Guest(&quot;John&quot;, &quot;Doe&quot;, 100, true, bookingDates, &quot;Breakfast&quot;);</pre><p>RestAssured can convert POJO to JSON as is, so we won’t need any additional steps. All we have to do is put our <em>booking </em>object in our request body:</p><pre>given()<br>    .contentType(&quot;application/json&quot;)<br>    .accept(&quot;application/json&quot;)<br>    .body(booking)<br>    .post(&quot;https://restful-booker.herokuapp.com/booking&quot;)<br>    .then()<br>    .statusCode(200);</pre><p>This way we are sending the POST request with the following body:</p><pre>{<br>    &quot;firstname&quot; : &quot;John&quot;,<br>    &quot;lastname&quot; : &quot;Doe&quot;,<br>    &quot;totalprice&quot; : 100,<br>    &quot;depositpaid&quot; : true,<br>    &quot;bookingdates&quot; : {<br>        &quot;checkin&quot; : &quot;2021-08-31&quot;,<br>        &quot;checkout&quot; : &quot;2021-09-10&quot;<br>    },<br>    &quot;additionalneeds&quot; : &quot;Breakfast&quot;<br>}</pre><h3>Deserialization</h3><p>To showcase how the deserialization with POJO works we’ll create the same booking as before, deserialize the response and extract the value of <em>bookingid</em> and then request the details of the booking with this id to compare them with the JSON we’ve initially sent.</p><p>At first let’s take a look at the response we’re getting after we create a new booking:</p><pre>{<br>    &quot;bookingid&quot;: 36,<br>    &quot;booking&quot;: {<br>        &quot;firstname&quot;: &quot;John&quot;,<br>        &quot;lastname&quot;: &quot;Doe&quot;,<br>        &quot;totalprice&quot;: 100,<br>        &quot;depositpaid&quot;: true,<br>        &quot;bookingdates&quot;: {<br>            &quot;checkin&quot;: &quot;2021-08-31&quot;,<br>            &quot;checkout&quot;: &quot;2021-09-10&quot;<br>        },<br>        &quot;additionalneeds&quot;: &quot;Breakfast&quot;<br>    }<br>}</pre><p>As you can see this JSON structure became a bit more complicated since it gained one more level. Just as we did before with <em>Booking </em>and <em>BookingDates</em> classes, let’s create the new <em>BookingInfo </em>class:</p><pre>public class BookingInfo {<br><br>    private int bookingid;<br>    private Booking booking;<br><br>    public BookingInfo() {<br>    }<br><br>    public int getBookingid() {<br>        return bookingid;<br>    }<br><br>    public Booking getBooking() {<br>        return booking;<br>    }<br>}</pre><p>Also, we’ll need to add empty constructors to the classes we’ve already created:</p><pre>public BookingDates() {<br>}</pre><p>and:</p><pre>public Booking() {<br>}</pre><p>Now we can save the response to our POST request as a variable and deserialize it at the same time:</p><pre>BookingInfo bookingInfo = given()<br>    .contentType(&quot;application/json&quot;)<br>    .accept(&quot;application/json&quot;)<br>    .body(body)<br>    .post(POST_URI)<br>    .then()<br>    .extract().body().as(BookingInfo.class);</pre><p>As you can see, deserialization is quite similar to serialization — RestAssured can also convert JSON to POJO without any additional steps.</p><p>Now we have the instance of the <em>BookingInfo </em>class which contains all the data we received. In order to check if all the data has been saved properly let’s use the <em>bookingid </em>we received to get our booking’s details:</p><pre>Booking checkBooking = given()<br>    .get(&quot;https://restful-booker.herokuapp.com/booking/&quot; + bookingInfo.getBookingid())<br>    .then()<br>    .extract().body().as(Booking.class);</pre><p>The response data was correct but since we don’t want to rely just on the visual check, let’s compare it to the data we’ve sent in our POST request.</p><p>The easiest way to do so is to convert our POJO to a string. In order to do that let’s override the<em> toString()</em> method in the <em>Booking </em>and <em>BookingDates </em>classes:</p><pre>@Override<br>public String toString() {<br>    return &quot;firstname: &quot; + this.firstname + &quot;; lastname: &quot; + this.lastname + &quot;; totalprice: &quot; + this.totalprice + &quot;; depositpaid: &quot; + this.depositpaid + &quot;; bookingdates: &quot; + this.bookingdates + &quot;; additionalneeds: &quot; + this.additionalneeds;<br>}</pre><p>and:</p><pre>@Override<br>public String toString() {<br>    return &quot;checkin: &quot; + this.checkin + &quot;; checkout: &quot; + this.checkout;<br>}</pre><p>After that all we have to do is to compare the strings we’ve got:</p><pre>Assert.assertEquals(checkBooking.toString(), bookingInfo.getBooking().toString());</pre><h3>Useful tools and libraries</h3><p><a href="http://twitter.com/Getter"><strong>@Getter</strong></a><strong> and </strong><a href="http://twitter.com/Setter"><strong>@Setter</strong></a><strong> annotations from the Lombok library</strong></p><p>Both getter and setter methods take quite a lot of space and barely bring any useful information with themselves — especially with larger POJOs where they can take up dozens of lines. Lombok library allows you to get rid of all getters and setters completely and replace them with two simple annotations:</p><p>Lombok:</p><pre>@Getter @Setter<br>public class BookingDates {<br>    private String checkin;<br>    private String checkout;<br>}</pre><p>Vanilla Java:</p><pre>public class BookingDates {<br>    private String checkin;<br>    private String checkout;<br><br>    public String getCheckin() {<br>        return checkin;<br>    }<br><br>    public String getCheckout() {<br>        return checkout;<br>    }<br><br>    public void setCheckin(String checkin) {<br>        this.checkin = checkin;<br>    }<br><br>    public void setCheckout(String checkout) {<br>        this.checkout = checkout;<br>    }<br>}</pre><p><strong><em>Jackson</em></strong></p><p><a href="http://twitter.com/JsonInclude"><strong>@JsonInclude</strong></a></p><p>JsonInclude annotation allows you to manage arguments which will be included or excluded from final JSON. You can use this annotation both with the whole class and with specific variables. Here are some values you can use with it: <em>ALWAYS </em>will include variable no mater what, <em>NON_NULL </em>will exclude variables that are equal to null and <em>NON_EMPTY </em>will exclude null values as well as empty lists and arrays.</p><p>For example, annotation <a href="http://twitter.com/JsonInclude"><em>@JsonInclude</em></a><em>(JsonInclude.Include.NON_NULL) </em>used for the whole POJO class will allow you to use the same class for multiple different JSON schemas. You’ll just have to set the variables you won’t need to null and they’ll be excluded from the final JSON.</p><p><a href="http://twitter.com/JsonProperty"><strong>@JsonProperty</strong></a></p><p>JsonProperty annotation allows you to state which getter or setter should be associated with a variable if you have to use a method with a different name for some reason:</p><pre>private String checkin;<br><br>@JsonProperty(&quot;checkin&quot;)<br>public String getDate() {<br>    return checkin;<br>}</pre><p><a href="http://twitter.com/JsonAlias"><strong>@JsonAlias</strong></a></p><p>JsonAlias allows you to save arguments with different names into the same variable:</p><pre>@JsonAlias({ &quot;firstName&quot;, &quot;name&quot; })<br>private String firstname;</pre><p>In this case the <em>firstname </em>variable will be used for all three field titles: <em>firstname</em>, <em>firstName </em>и <em>name</em>. This annotation helps quite a lot with inconsistent JSON schemas where same values are used with different names.</p><p><a href="http://twitter.com/JsonIgnoreProperties"><strong>@JsonIgnoreProperties</strong></a><strong> и </strong><a href="http://twitter.com/JsonIgnore"><strong>@JsonIgnore</strong></a></p><p>Both of these annotations allow you to state which variables should be ignored while serializing or deserializing JSON.</p><p><a href="http://twitter.com/JsonIgnoreProperties">@JsonIgnoreProperties</a> is used for the whole class:</p><pre>@JsonIgnoreProperties({ &quot;bookingid&quot; })<br>public class BookingInfo {<br><br>    private int bookingid;<br>    private Booking booking;<br>}</pre><p><a href="http://twitter.com/JsonIgnore">@JsonIgnore</a> is used for the variable that should be excluded:</p><pre>private int bookingid;<br>@JsonIgnore<br>private Booking booking;</pre><p><strong><em>Package org.hamcrest.beans from the Hamcrest library</em></strong></p><p><strong>hasProperty</strong></p><p><em>hasProperty()</em> method allows you to check if an instance of a class contains said property:</p><pre>assertThat(bookingInfo, hasProperty(&quot;bookingid&quot;))</pre><p>or:</p><pre>assertThat(bookingInfo, hasProperty(&quot;bookingid&quot;, equalTo(15))</pre><p><strong>samePropertyValuesAs</strong></p><p>samePropertyValuesAs() allows you to compare two instances of the same or different classes:</p><pre>assertThat(bookingInfo, samePropertyValuesAs(bookingInfoCheck))</pre><p>Though it’s important to mention that this method works correctly only with simple non-nested POJOs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd15b620cdda" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>