REST Assured API Automation Testing Ⅰ - Getting Started

简介: REST Assured API Automation Testing Ⅰ - Getting Started

Section 1: What is Rest Assured?

Rest Assured is a Java Library that can testing Apis, also called RESTful Apis. You can write test cases using the given, when, then which human are readable and can be easily understood.

As introduced on the official website REST Assured brings the simplicity of using dynamic languages into the Java domain. Rest Assured uses groovy under the hood. Rest Assured can used along with unit testing framework like Junit, TestNG and also with the Cucumber BDD which BDD stand for Behavioral Driver Development.

You can find more documents on the Rest Assured homepage.

Section 2: Setup for Rest Assured Project

Below is the list of tools or libraries we gonna need to creating a Rest Assured project.

  • Java JDK - 8
  • TestNG
  • IntelliJ
  • Maven

We gonna use JDK - 8 as the default version of Java and the TestNG for the Unit Testing framework, IntelliJ as the IDE and build the project by Maven 3.6.3.

Launch the IntelliJ and click Create New Project

image.png

Choose maven to build the Project and click Next button

image.png

Then fill the project name and GoupId, ArtifactId, then click Finish button

image.png

pom.xml is the place where we add dependencies, first we created dependencies tag, then add Rest Assured and TestNG dependencies, you check the offical doucment Getting Stated page or search from the maven central repository. Below are the REST Assured and TestNG dependencies.

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.6.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>
复制代码

REST Assured includes JsonPath and XmlPath as transitive dependencies, so we don't have to add those again, and you can check the libaries form External Dependices that automatically download by IntelliJ.

Create new package under test package named com.restassured, create a java class named QuickstartTest then create a Java class testAlpha and add TestNG annotation @Test.

public class QuickstartTest {
    @Test
    public void testAlpha(){
    }
}
复制代码

We need to add the import, but as static import. Importing these rest assured classes as static imports has some advantages even though it's generally not recommendedto import classes as static imports, then we can using the given when then statement

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
public class QuickstartTest {
    @Test
    public void testAlpha(){
        given().
                when().
                then();
    }
}
复制代码

then Run the testAlpha method, we got the test reslut on the console bottom of the IntelliJ

image.png

Why use static imports

with static import, we don't have to use the class name along with the middle name. The given method class name is RestAssured, we are using static keyword for importing the classes

image.png


相关文章
|
JSON API 开发工具
【Azure 应用服务】调用Azure REST API来获取 App Service的访问限制信息(Access Restrictions)以及修改
【Azure 应用服务】调用Azure REST API来获取 App Service的访问限制信息(Access Restrictions)以及修改
317 1
|
存储 API 网络架构
【Azure 存储服务】调用REST API获取Stroage Account Table中所有的Entity计数 -- Count
【Azure 存储服务】调用REST API获取Stroage Account Table中所有的Entity计数 -- Count
304 1
|
缓存 API 网络架构
掌握现代API开发:GraphQL vs REST
【10月更文挑战第24天】本文深入探讨了现代API开发中两种主流技术——GraphQL和REST的设计理念、技术特点及实际开发中的对比分析。GraphQL通过声明式数据请求和强类型系统提供更高的灵活性和性能,而REST则以其无状态特性和成熟的生态系统见长。文章还讨论了两者在客户端-服务器交互、安全性和工具支持方面的优劣,帮助开发者根据项目需求做出明智选择。
|
JSON 中间件 API
开发REST API3-11
开发REST API3-11
|
JSON JavaScript API
编写REST API
编写REST API
390 2
|
存储 JSON API
【Azure 存储服务】使用REST API操作Azure Storage Table,删除数据(Delete Entity)
【Azure 存储服务】使用REST API操作Azure Storage Table,删除数据(Delete Entity)
316 0
【Azure 存储服务】使用REST API操作Azure Storage Table,删除数据(Delete Entity)
|
Java API Maven
使用 Smart-doc 记录 Spring REST API
使用 Smart-doc 记录 Spring REST API
888 0
|
XML 安全 API
REST 和 SOAP API 有什么区别?
【8月更文挑战第31天】
1464 0
|
JSON API 数据安全/隐私保护
哇塞!Django REST framework 太逆天啦!构建 API 服务从未如此轻松,你还不来试试?
【8月更文挑战第31天】Django REST framework(DRF)是基于Django框架的高效Web API开发工具,提供序列化、视图集、路由等功能,简化API构建流程。使用DRF可轻松实现数据的序列化与反序列化,并支持权限管理和认证机制以保障API安全。安装DRF只需通过`pip install djangorestframework`命令。要创建基本项目,先安装Django并创建新应用,定义模型、序列化器及视图集,最后配置路由。测试API时,可通过Postman发送HTTP请求验证功能。无论项目大小,DRF均能提供强大支持。
467 0