导入静态io.rest assured.RestAssured.*;不工作

e5nszbig  于 2022-09-20  发布在  Maven
关注(0)|答案(7)|浏览(135)

我正在尝试编写一个用于API测试的BDD框架。导入静态io.rest assured.RestAssured.*;不工作。此外,当我使用给定()关键字时,它会抛出错误。我已经添加了各自的maven依赖项,但静态导入不起作用。不了解需要添加什么或需要更改添加的依赖项。

代码片段:Package rest Api;

import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matcher.*;

public class Test1_BasicFeature

{

@Test
public void testStatusCode()
{
    given().

}

}

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.servicetest</groupId>
<artifactId>maven-servicetest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
ttvkxqim

ttvkxqim1#

我可以看到您已经将RESTAssured的作用域设置为在pom.xml中进行测试

"<scope>test</scope>"

这意味着除了测试之外,您不能在其他任何地方使用库。去掉它,使作用域默认为项目。即

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
</dependency>
r1wp621o

r1wp621o2#

使用

import static io.restassured.*;
import static org.hamcrest.*;

而不是

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matcher.*;
zengzsys

zengzsys3#

在您的测试中添加以下内容:

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static io.restassured.RestAssured.then;

这也可能解决一些问题。

dfty9e19

dfty9e194#

open your pom.xml file and change

<dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.4.0</version>
            <scope>compile</scope>
        </dependency>
rkue9o1l

rkue9o1l5#

如果给定()抛出编译时间,则使用RestAssured.given()

RestAssured.given().get(url).getStatusCode();
mccptt67

mccptt676#

如果您添加了依赖项,但仍收到错误消息

  • 只需构建您的项目。*

路径(Mac用户)步骤:

1.项目->
1.建设项目。

u1ehiz5o

u1ehiz5o7#

import static io.restassured.RestAssured.*;

这应该被声明为高于所有其他进口,如下所示:

import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
import static org.hamcrest.Matcher.*;

相关问题