Skip to main content

Day 2: Setting up Rest Assured

Day 2: Setting up Rest Assured

Rest Assured is a Java library used for testing RESTful APIs. Before we start using Rest Assured, we need to set it up in our project. In this post, we will learn how to set up Rest Assured in a Java project. There are different ways to set up Rest Assured, but we will be using Maven, a popular build automation tool for Java projects. If you are not familiar with Maven, you can learn more about it here: https://maven.apache.org/what-is-maven.html.


Step 1: Create a Maven project

To get started, we need to create a new Maven project. You can create a Maven project using an IDE like Eclipse or IntelliJ IDEA, or you can create a Maven project using the command line. If you are using the command line, you can use the following command.



mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  

This command will create a new Maven project with the following details:

  • groupId: com.example
  • artifactId: my-app

You can change the groupId and artifactId according to your project requirements.



Step 2: Add Rest Assured dependency

Once you have created a new Maven project, you need to add Rest Assured dependency in the pom.xml file. Open the pom.xml file and add the following dependency:



<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.3.3</version>
</dependency>


This dependency will download the Rest Assured library and its dependencies.


Step 3: Write your first Rest Assured test

Now that we have set up Rest Assured in our project, we can start writing our first Rest Assured test. Create a new Java class in the src/test/java folder and name it TestRestAssured. Add the following code to the class:


import io.restassured.RestAssured;
import org.junit.Test;

public class TestRestAssured {
    @Test
    public void testStatusCode() {
        RestAssured.given()
          .get("https://jsonplaceholder.typicode.com/posts")
          .then()
          .statusCode(200);
    }
}

This code sends a GET request to https://jsonplaceholder.typicode.com/posts and checks if the response status code is 200. You can run this test using a test runner like JUnit.

That's it! You have now set up Rest Assured in your project and written your first Rest Assured test.

Comments

Popular posts from this blog

How to integrate Autoit with selenium

For handling Dialog boxes which are not web based, Then Autoit is the best Tool to handle this These are the following code should written in Selenium Selenium Code: try { String[] commands = new String[] {}; commands = new String[] { "Path" }; // location of Autoit EXE file Runtime.getRuntime().exec(commands); } catch (IOException e) { } Autoit code: if WinWaitActive("File Upload") Then ;MsgBox(2,"window found","Found the window") WinActivate("File upload") Send("!n") Sleep(5000) Send("File path") SEND("{ENTER}") ;location of the file you want to a to the form and submit ;Send("!O") Else MsgBox(1,"TimeOut","Timed out") EndIf Here we need to create a Exe file for the Autoit script and that path should be mentioned in selenium code.

Reflection API

The Reflection API allows Java code to examine classes and objects at run time.The new reflection classes allow you to call another class's methods dynamically at run time. With the reflection classes, you can also examine an instance's fields and change the fields' contents. The Reflection API consists of the java.lang.Class class and the java.lang.reflect classes: Field, Method, Constructor, Array, and Modifier. Example program: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; // calling method whose name we store in a variable public class ReflectionAPI { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String x="sampleTest"; //String meth=read name from xls file Method method= ReflectionAPI.class.getMethod(x, String.class); method.invoke(method, "welcome"); System.out.

Collection API

CollectionAPI: The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects. In a sense Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays. Example program: import java.util.ArrayList; import java.util.Hashtable; public class CollectionAPI { /** * @param args */ public static void main(String[] args) { int names[] = new int[5]; ArrayList list = new ArrayList (); list.add("Ramu");//0 list.add("Venu");//1 list.add("Raju");//2 System.out.println(list.size()); for(int i =0; i<=list.size(); i++) { System.out.println(list.get(i)); } // key - value // key - unique Hashtable table = new Hashtable (); table.put("name", "Hyderabad"); table.put("place", "Mumbai"); table.put("na