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


相关文章
|
2月前
|
开发框架 监控 .NET
Visual Basic的Web服务和REST API开发指南
【4月更文挑战第27天】本文探讨了使用Visual Basic(VB.NET)构建Web服务和RESTful API的方法。首先介绍了Web服务的基础和REST API的概念,然后阐述了.NET Framework与.NET Core/.NET 5+对VB.NET的支持,以及ASP.NET Core在Web开发中的作用。接着,详细讲解了创建RESTful API的步骤,包括控制器与路由设置、模型绑定与验证,以及返回响应。此外,还讨论了安全措施、测试方法、部署选项和监控策略。最后强调,VB.NET开发者可以通过ASP.NET Core涉足现代Web服务开发,拓宽技术领域。
|
2月前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
87 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
2月前
|
前端开发 JavaScript API
基于React的简易REST API客户端设计与实现
基于React的简易REST API客户端设计与实现
46 3
|
5天前
|
设计模式 缓存 JavaScript
API设计模式:REST、GraphQL、gRPC与tRPC全面解析
API设计模式:REST、GraphQL、gRPC与tRPC全面解析
12 0
|
2月前
|
缓存 前端开发 API
构建可扩展的API:REST vs GraphQL
【5月更文挑战第14天】在API设计中,REST和GraphQL是两种主要的架构风格。REST基于HTTP协议,强调资源的无状态性和客户端-服务器模型,适合简单CRUD操作。而GraphQL提供更灵活的数据查询,允许客户端精确指定需求,减少HTTP请求和数据冗余,适合复杂场景和高性能需求。选择时要考虑项目需求、技术栈、性能和团队经验。
|
2月前
|
Prometheus 监控 Cloud Native
第七十一章 使用 REST API 监控 IRIS - 活动量指标
第七十一章 使用 REST API 监控 IRIS - 活动量指标
24 0
|
2月前
|
监控 API 网络架构
第七十章 使用 REST API 监控 IRIS - 互操作性指标
第七十章 使用 REST API 监控 IRIS - 互操作性指标
24 0
|
2月前
|
SQL 监控 API
第六十九章 使用 REST API 监控 IRIS
第六十九章 使用 REST API 监控 IRIS
24 0
|
2月前
|
API 网络架构 开发者
邮箱REST API发送邮件有什么方法步骤?
AokSend指南:通过邮箱REST API发送邮件涉及6步。首先,研究邮箱服务提供商的API文档;其次,获取API密钥和身份验证信息;然后,用编程语言构建包含收件人、发件人、主题、正文和附件的API请求;接着,使用HTTP POST发送请求到API端点;收到响应后处理邮件发送状态;最后,进行错误处理。AokSend提供高效稳定的触发式及SMTP/API接口,支持大量验证码发送。在使用过程中,遵守服务条款和隐私政策。
|
2月前
|
分布式计算 DataWorks 关系型数据库
DataWorks操作报错合集之在DataWorks同步数据时,遇到乱码问题,该怎么解决(rest api数据源)
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
40 0