首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >REST Assured API Automation Testing Ⅰ - Getting Started

REST Assured API Automation Testing Ⅰ - Getting Started

作者头像
RiemannHypothesis
发布2022-09-26 21:13:48
发布2022-09-26 21:13:48
35200
代码可运行
举报
文章被收录于专栏:ElixirElixir
运行总次数:0
代码可运行

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情

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

Choose maven to build the Project and click Next button

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

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.

代码语言:javascript
代码运行次数:0
运行
复制
<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.

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Section 1: What is Rest Assured?
  • Section 2: Setup for Rest Assured Project
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档