Skip to main content

Day 3: Writing your first test with Rest Assured

Day 2: Setting up Rest Assured Day 3: Writing your first test with Rest Assured

In this topic, we will learn how to create a simple test using Rest Assured. The test will send a request to an API endpoint and verify the response. We will also learn about basic Rest Assured syntax and assertions.


  1. Creating a Simple Test

  2. To create a simple test, we need to first identify an API endpoint that we want to test. We will use the GET method to send a request to this endpoint. We can then use Rest Assured to extract data from the response and perform assertions on it.


    Here is an example code snippet that shows how to create a simple test using Rest Assured:

    
    @Test
    public void testGetUsers() {
        given()
            .when().get("https://myapi.com/users")
            .then().statusCode(200);
    }
    
    


    In this example, we are using the given(), when(), and then() methods to structure our test. We first use given() to set up our request, when() to send the request, and then() to perform assertions on the response. In this case, we are checking that the response has a status code of 200.



  3. Basic Rest Assured Syntax
  4. Rest Assured has a simple and intuitive syntax that allows us to easily set up requests and perform assertions on the response. Here are some basic Rest Assured syntax examples:

    Setting Request Headers:
    
    given()
        .header("Content-Type", "application/json")
    
    
    Setting Query Parameters:
    
    given()
        .queryParam("name", "John")
    
    
    Sending a Request:
    
    when()
        .get("https://myapi.com/users")
    
    
    Performing Assertions:
    
    then()
        .statusCode(200)
        .body("name", equalTo("John"))
    
    
  5. Assertions

Rest Assured provides a variety of assertion methods that we can use to validate our API responses. Here are some commonly used assertion methods:

  • statusCode(int code) - checks the status code of the response
  • body(String path, Matcher(?) matcher) - checks the value of a specific field in the response body
  • contentType(ContentType contentType) - checks the content type of the response
  • header(String name, String value) - checks the value of a specific header in the response

In conclusion, we learned how to create a simple test using Rest Assured, basic Rest Assured syntax, and assertions. These concepts are fundamental to understanding how Rest Assured works and how we can use it to test our APIs.

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