Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas! Welcome to this blog, where we will discuss REST Assured – Static Import in Java.
Here in this article, we have covered the definitions of REST assured the need for it, static import with an example, and static member in a class too. Let's start our topic REST Assured – Static Import in Java by firstly knowing about REST Assured.
REST Assured
Rest Assured is a Java library that allows you to test and validate REST(Representational State Transfer) services, APIs(application program interface), and Web Services. Previously, API testing was more complex than with Ruby and Groovy. Rest assured provides that simplicity and flexibility in Java.
It is a platform that is open source. It is based on HTTP Builder and can handle POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests. Now for the topic REST Assured – Static Import in Java, we will cover the need for Rest assured.
Why is Rest-Assured Needed❓
Imagine you open Google Maps and search for a location, you immediately see nearby restaurants, commute options from some leading travel providers, and so many other options at your fingertips.
We all know they are not Google products, so how does Google display them? They make use of these providers' exposed APIs. Testing APIs becomes critical if you are asked to test this type of setup even before the UI is built or in development. Testing them repeatedly with different data combinations makes it a very suitable case for automation.
Previously, we used dynamic languages like groovy and ruby to accomplish this, which was difficult. As a result, API testing was not investigated by functional testing.
However, if one has a basic understanding of Java, using Rest Assured, automation testing of APIs, and sending simple HTTP requests with user-friendly customizations is simple. It is necessary to understand API testing and integration testing. Still, after that, Rest Assured provides very good confidence on the backend, allowing front-end testing to focus solely on the UI and client-side operations. Rest Assured is open source, and adding many new methods and libraries has made it an excellent choice for API automation.
What is Static Import❓
A static import allows public and static members of a class, such as fields and methods, to be used in code without specifying the class name. Version 5 of the java language included this feature.
Use the following syntax if you want to import all static and public members of a class.
import static packageName.className.*;
You can also try this code with Online Java Compiler
Here "java.lang" is the package name and "Math" is the class name.
The distinction between import and static import:
Import
Static Import
The standard import declaration imports classes from packages, allowing them to be used without requiring package qualification.
The import declaration is static. Static members from classes are imported, allowing them to be used without class qualification.
The class or method should be accessible as well. The access modifiers determine accessibility.
A static import declaration can take two forms: one that imports a single static member (single static import) and one that imports all static members of a class (static import on demand).
Java Static Import with Example
Without utilizing the fully qualified name, static import enables direct access to a class' static member.
You need to be familiar with Java packages to grasp this topic. You can save time by typ1`ing less by using static imports. Such imports might be intriguing if you detest typing the same stuff repeatedly.
Let's use examples to help us grasp this.
Example 1:
// Without the use of Static Imports
class Ninja
{
public static void main(String args[])
{
double example_1= Math.sqrt(10.0);
System.out.println("Square of 10 is:"+ example_1);
double example_2= Math.tan(50);
System.out.println("Tan of 50 is:"+ example_2);
}
}
You can also try this code with Online Java Compiler
// With the use of Static Imports
import static java.lang.System.out;
import static java.lang.Math.*;
class Demo2
{
public static void main(String args[])
{
// Use sqrt in place of Math.sqrt.
double example_1= sqrt(10.0);
out.println("Square of 5 is:"+example_1);
// Use merely tan instead of Math.tan.
double example_2= tan(50);
out.println("Tan of 30 is:"+example_2);
}
}
You can also try this code with Online Java Compiler
As much as possible, avoid the use of static import. It is useful when you need static members from only one or two classes.
We can use static import to directly approach the static members of a class without using the class name or any object.
For example, we always use the sqrt() method of the Math class by using Math. sqrt(), but by using static import, we can directly access the sqrt() method.
The excessive use of static import reduces readability and confusion.
However, the static import concept was introduced to improve code readability or eliminate code duplication.
Code for REST Assured – Static Import in Java
// Code for REST Assured – Static Import in Java.
package javaPackageName;
import static io.restassured.RestAssured.*;
public class StaticImportRestAssured
{
final static String url="https://reqres.in/api/users/2";
public static void main(String args[])
{
getResponseBody();
getResponseStatus();
}
// This will fetch the response body as it is and log it.
public static void getResponseBody()
{
given().when().get(url).then().log().all();
given().when().get("https://reqres.in/api/users/2").then().log().body();
}
// This will fetch the response status.
public static void getResponseStatus()
{
int statusCode= given().when().get("https://reqres.in/api/users/2").getStatusCode();
System.out.println("The response status is "+statusCode);
given().when().get(url).then().assertThat().statusCode(200);
}
}
You can also try this code with Online Java Compiler
Code for REST Assured – Static Import in Java, most of the time we use all the static methods of REST Assured using the class reference of REST Assured classes. But we can use the static method of REST Assured classes using static import. Here in the above code, we created a class named "StaticImportRestAssured". Under this class, we created a string variable("url" of final static type). Then we defined two methods "getResponseBody"and "getResponseStatus" to get the response body and response status respectively.
A static block in a Java class is a set of instructions executed once only when the class is loaded into memory. Static blocks are also known as static initialization blocks.
Can we overload static methods in Java?
A subclass cannot override a static method resolved at compile time. Overriding an instance method that is determined at runtime is possible. Overloading a static method is feasible.
Where is REST assured used?
Rest Assured is used to validate REST APIs using the Java library. The Java library acts as a headless client to interact with Rest web services. It is based on the Rest Assured library and can validate the server's HTTP responses.
Is it possible to inherit a static class?
Since static classes are sealed, they cannot be inherited. They cannot inherit from any other class than Object. An instance function Objects not permitted in static types. They may, however, include a static constructor.
In Java, what is a static keyword?
The static keyword is a method and attributes a non-access modifier. Static methods and attributes can be accessed without creating a class object.
Conclusion
In this blog, we have learned about REST Assured – Static Import in Java. The articles contain the proper definition of algorithms with output and programming.
If you found this blog has helped you enhance your knowledge, and if you want to learn more algorithms like the REST Assured – Static Import in Java, check out our articles below: