<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luiz Eduardo Martins</title>
    <description>The latest articles on DEV Community by Luiz Eduardo Martins (@luuizeduardo).</description>
    <link>https://dev.to/luuizeduardo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F987267%2F5e9a2075-83a5-4da8-8d57-0c687ba23d9e.jpeg</url>
      <title>DEV Community: Luiz Eduardo Martins</title>
      <link>https://dev.to/luuizeduardo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luuizeduardo"/>
    <language>en</language>
    <item>
      <title>API Testing with Java and Spring Boot Test - Part 2: Improving the solution</title>
      <dc:creator>Luiz Eduardo Martins</dc:creator>
      <pubDate>Tue, 07 Feb 2023 08:06:43 +0000</pubDate>
      <link>https://dev.to/mercedesbenzio/api-testing-with-java-and-spring-boot-test-part-2-improving-the-solution-24li</link>
      <guid>https://dev.to/mercedesbenzio/api-testing-with-java-and-spring-boot-test-part-2-improving-the-solution-24li</guid>
      <description>&lt;p&gt;In the last part of this step-by-step, we created the project, set up the test framework and also did all the configurations needed to run our API tests.&lt;/p&gt;

&lt;p&gt;You can see the first part of the series here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/luuizeduardo/api-testing-with-java-and-spring-boot-test-part-1-the-basic-setup-5db0"&gt;API Testing with Java and Spring Boot Test - Part 1: The basic setup&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's continue to grow our test framework, but first, we need to do some improvements to the existing code. In this guide, we'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refactor the object mapping (to be easier to handle with the JSON files)&lt;/li&gt;
&lt;li&gt;Improve the response validations
&lt;/li&gt;
&lt;li&gt;Handle multiple environments inside our tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These changes will let our code base cleaner and easier to maintain while helping us to create a scalable framework of API tests.&lt;/p&gt;

&lt;p&gt;Let's do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.tenor.com%2F4i_vCC1wVw4AAAAC%2Fhomer-simpson-woohoo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.tenor.com%2F4i_vCC1wVw4AAAAC%2Fhomer-simpson-woohoo.gif" alt="Homer Simpson woo hoo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring the Object mapping
&lt;/h2&gt;

&lt;p&gt;We'll take advantage of using the Spring boot &lt;code&gt;Repository&lt;/code&gt; to separate the responsibility of mapping the objects (JSON) we're going to use inside our tests. That way, we can do another step forward in our code cleanup.&lt;/p&gt;

&lt;p&gt;So, first of all, we're going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new package called &lt;code&gt;repositories&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Then we create a new &lt;code&gt;Class&lt;/code&gt; inside this package called &lt;code&gt;FileUtils&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also take the opportunity to change the way we map the object to not be hard-coded but be in a proper resource file. That way when we need to change the test data, we don't have to change the test but only the correspondent resource file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see in the file above, we created a function to read a JSON file and then return the object already mapped - similar to the approach we had before in the test file.&lt;/p&gt;

&lt;p&gt;Now, we'll structure the &lt;code&gt;resources&lt;/code&gt; folder to accommodate the JSON files.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;resources&lt;/code&gt; folder, let's create a new directory called &lt;code&gt;user&lt;/code&gt; and then create a file to store the request body of the operation we'll do.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After that, we need to update our test. Now we want to get the file data by using the new function we created for that purpose. &lt;/p&gt;

&lt;p&gt;The updated test will look like that:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Much better! By keeping the code cleaner, we are helping our future selves with its maintenance - trust me, you'll be very glad to see this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Improving the response validation
&lt;/h2&gt;

&lt;p&gt;Great! Now, let's have a look at the response validation. &lt;/p&gt;

&lt;p&gt;In some cases, we want to check the full response body - or at least some parts of it - to fulfill the test requirements.&lt;/p&gt;

&lt;p&gt;To do that, we'll create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new &lt;code&gt;Repository&lt;/code&gt; to abstract the responsibility and help us check the full JSON response body&lt;/li&gt;
&lt;li&gt;A function to handle the check of the JSON response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also add the "jsonassert" dependency to assert the JSON.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pom.xml&lt;/code&gt; file will look like that:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The newly created &lt;code&gt;ResponseUtils&lt;/code&gt; class will be something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The next step should be to use this new function and improve our test. To do that, we'll configure a GET request on &lt;code&gt;YourApiService&lt;/code&gt; and return the full &lt;code&gt;Response&lt;/code&gt; object. Then we should be able to check the response body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;RestAssured&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;when&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, it's just a matter of adding the test case to the &lt;code&gt;ApiTest&lt;/code&gt; test class and using the same strategy of letting the JSON response file be in its proper directory. Finally, we'll have something like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testGetUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;JSONException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;yourApiService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;JsonNode&lt;/span&gt; &lt;span class="n"&gt;expectedResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FileUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readJsonFromFile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"responses/user/specific.json"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;equalTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
    &lt;span class="nc"&gt;ResponseUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertJson&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expectedResponse&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Quite easy to understand if you just look at the test case :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.tenor.com%2FTRjZAIXAxL8AAAAC%2Fdonut-simpson.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.tenor.com%2FTRjZAIXAxL8AAAAC%2Fdonut-simpson.gif" alt="Homer Simpson relieved"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Executing the tests over multiple environments
&lt;/h2&gt;

&lt;p&gt;Now we have the tests properly set, and everything is in the right place. One thing that could be in your mind right now is: "Ok, but I have a scenario in my product in which I need to run my test suit over multiple environments. How do I do that?". &lt;/p&gt;

&lt;p&gt;And the answer is - property files.&lt;/p&gt;

&lt;p&gt;The property files are used to store specific data which we can use along our test suit, like the application host, port, and path to the API. You can also store environment variables to use within your test framework. However, be careful since we don't want to make this information public. You can see an example in the lines below.&lt;/p&gt;

&lt;p&gt;With Spring boot, we take advantage of using the "profiles" to set the specifics of the environments our application has, and make them available as Spring boot profiles.&lt;/p&gt;

&lt;p&gt;So, let's do that. Inside the &lt;code&gt;resources&lt;/code&gt; folder, we'll create a new file called &lt;code&gt;application-prod.properties&lt;/code&gt; to store the values of the production cluster of the test application. The file will store something like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;apitest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nl"&gt;https:&lt;/span&gt;&lt;span class="c1"&gt;//reqres.in&lt;/span&gt;
&lt;span class="n"&gt;apitest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;
&lt;span class="n"&gt;apitest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="no"&gt;TOKEN&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, the only thing missing is to change our service to get the values stored in the property file. &lt;/p&gt;

&lt;p&gt;To get the values from the property files, we'll use the annotation &lt;code&gt;@Value&lt;/code&gt;. This annotation will provide the values from the properties we set in the &lt;code&gt;application-prod.properties&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Bear in mind: *&lt;/em&gt; You'll need to set the environment variable before using it here. The &lt;code&gt;@Value&lt;/code&gt; annotation will grab this value from the environment variables you have set.&lt;/p&gt;

&lt;p&gt;The updated version of &lt;code&gt;YourApiService&lt;/code&gt; class will look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;That's a great step up. This way, if you have multiple environments in your setup, you just need to create another &lt;code&gt;application-YOUR_PROFILE_NAME.properties&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Executing the test suit
&lt;/h2&gt;

&lt;p&gt;You must be wondering: How do I run the test suit with this newly created profile? &lt;/p&gt;

&lt;p&gt;The answer is simple, just execute &lt;code&gt;mvn clean test -Dspring.profiles.active=prod&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By default, if you just run the &lt;code&gt;mvn clean test&lt;/code&gt; command, Spring Boot will try to find a file called &lt;code&gt;application.properties&lt;/code&gt; and automatically activate it.&lt;/p&gt;

&lt;p&gt;Now we have significantly improved the test setup of our application by: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The refactoring of the Object mapping to clean up our code and apply some best practices&lt;/li&gt;
&lt;li&gt;Improving the response validation by adding a new dependency and using it to simplify the check&lt;/li&gt;
&lt;li&gt;Learning how to handle multiple test environments. This should be useful when it comes to companies that have layers of environments before the code reach production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Are you curious about the article? Building a Java API test framework part 3 will further improve our application. We will then go deeper into the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test reporting with Allure reports&lt;/li&gt;
&lt;li&gt;Configure a CI pipeline with GitHub actions&lt;/li&gt;
&lt;li&gt;Publish the test report on GitHub pages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you soon!&lt;/p&gt;

</description>
      <category>java</category>
      <category>restassured</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>API Testing with Java and Spring Boot Test - Part 1: The basic setup</title>
      <dc:creator>Luiz Eduardo Martins</dc:creator>
      <pubDate>Fri, 16 Dec 2022 14:12:08 +0000</pubDate>
      <link>https://dev.to/mercedesbenzio/api-testing-with-java-and-spring-boot-test-part-1-the-basic-setup-5db0</link>
      <guid>https://dev.to/mercedesbenzio/api-testing-with-java-and-spring-boot-test-part-1-the-basic-setup-5db0</guid>
      <description>&lt;p&gt;At Mercedes-benz.io (MB.io), we collaborate as multiple multi-disciplinary teams (nothing new to a Scrum-based organization). &lt;/p&gt;

&lt;p&gt;I'm part of one of those teams, responsible for a Java-based microservice. Since this microservice sends data to a back-office application, we need to test the APIs provided by it.&lt;/p&gt;

&lt;p&gt;With that said, we had the challenge to build a new API test framework from scratch. &lt;/p&gt;

&lt;p&gt;In this series of articles we'll show: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How we choose the tools&lt;/li&gt;
&lt;li&gt;The process of creating and improving the test framework&lt;/li&gt;
&lt;li&gt;Pipeline configuration&lt;/li&gt;
&lt;li&gt;Test report&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3oEjI5VtIhHvK37WYo/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3oEjI5VtIhHvK37WYo/giphy.gif" alt="Eddie Murphy's OK gesture" width="600" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the language and framework
&lt;/h2&gt;

&lt;p&gt;The main reason why we went for a Java-based framework is that the background of our team is Java and the microservice itself is written in this language. Our team is composed of Java developers, so they can contribute to building the right solution for our team and also maintain the code base of the test repository in case it's needed.&lt;/p&gt;

&lt;p&gt;The test framework we've chosen to be the base of our solution was &lt;a href="https://rest-assured.io/"&gt;Rest Assured.io&lt;/a&gt;. The reason behind it is that rest assured is already used in several projects within our tribe at MB.io and is also widely used and maintained in the community.&lt;/p&gt;

&lt;p&gt;We also added &lt;a href="https://spring.io/"&gt;Spring Boot&lt;/a&gt; to organize, structure and be the foundation of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the project
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/fKujfcYtnod4R6soBF/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/fKujfcYtnod4R6soBF/giphy.gif" alt="let's go gif" width="480" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create the project
&lt;/h3&gt;

&lt;p&gt;We choose &lt;a href="https://maven.apache.org/"&gt;Maven&lt;/a&gt; as our dependencies manager. Now, the first thing to do is to add the dependencies we need in our project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: You can use &lt;a href="https://start.spring.io/"&gt;the spring boot initializer&lt;/a&gt; to get the basic &lt;code&gt;pom.xml&lt;/code&gt; file with the spring initial setup.&lt;/p&gt;

&lt;p&gt;After the initial setup, we need to add the dependencies for the rest-assured test framework and other things we'll use to make our lives easier. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pom.xml&lt;/code&gt; file should be something like that:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With this, we should be able to start organizing our project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Changing the &lt;code&gt;Main&lt;/code&gt; class
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Main&lt;/code&gt; class should be changed to a &lt;code&gt;SpringBootApplication&lt;/code&gt;. And the main method must be configured to run as a &lt;code&gt;SpringApplication&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 3: Create a &lt;code&gt;Service&lt;/code&gt; to manage your API
&lt;/h3&gt;

&lt;p&gt;To abstract access and configure the requests in one single place, we can create a new &lt;code&gt;Service&lt;/code&gt; and take advantage of it.&lt;/p&gt;

&lt;p&gt;Here is the place to set the proper configuration of the requests.&lt;/p&gt;

&lt;p&gt;Let's create a new method here to abstract the use of a post request. In this post request, we'll provide the URL and the JSON body as parameters, so the file will be something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We'll return the full response to be able to validate what we want within the test itself.&lt;/p&gt;

&lt;p&gt;As you can see in the file above, we also take advantage of the built-in &lt;code&gt;RequestSpecification&lt;/code&gt; that Rest-assured has to set the &lt;strong&gt;&lt;em&gt;baseURI&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;basePath&lt;/em&gt;&lt;/strong&gt; for this service. This is a smart way to configure your service because if you have more than one service in our test framework, each of them can have its setup and host.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Add a test case
&lt;/h3&gt;

&lt;p&gt;First things first, let's add the proper annotations to be a spring boot JUnit 5 test class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ExtendWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SpringExtension&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@SpringBootTest&lt;/span&gt;
&lt;span class="nd"&gt;@TestInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TestInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Lifecycle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PER_CLASS&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After that, let's add a constructor method and assign the &lt;code&gt;Service&lt;/code&gt; to be used in our test as a class variable.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;YourApiService&lt;/span&gt; &lt;span class="n"&gt;yourApiService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ApiTest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;YourApiService&lt;/span&gt; &lt;span class="n"&gt;yourApiService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;yourApiService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;yourApiService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we are good to start adding the test cases here. Let's do that.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;postRequest&lt;/code&gt; method expects two parameters: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the endpoint we want to send the data as a String;&lt;/li&gt;
&lt;li&gt;the request body as a JsonNode. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first thing we want to do is create an object to send in the request body of our request. We'll take advantage of the &lt;code&gt;jackson-databind&lt;/code&gt; library to help us with the object mapping.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testCreateUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;JsonProcessingException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;ObjectMapper&lt;/span&gt; &lt;span class="n"&gt;mapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{\"name\": \"Luiz Eduardo\", \"job\": \"Senior QA Engineer\"}"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;JsonNode&lt;/span&gt; &lt;span class="n"&gt;requestBody&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readTree&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, we need to make the request and validate what we want. Let's add that to our test case. The final results should be something like that:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Bear in mind that this is just the first iteration, we'll improve the code to keep the responsibilities within the respective classes.&lt;/p&gt;

&lt;p&gt;What we've seen so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The project's basic setup&lt;/li&gt;
&lt;li&gt;How to structure the project&lt;/li&gt;
&lt;li&gt;How to abstract the requests in the service layer&lt;/li&gt;
&lt;li&gt;The first functional test case&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the end of part 1 of this series of articles. The next part will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A short refactor on the object mapping&lt;/li&gt;
&lt;li&gt;Improve the response validation&lt;/li&gt;
&lt;li&gt;Property files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you soon!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>java</category>
      <category>restassured</category>
    </item>
  </channel>
</rss>
