import static io.restassured.RestAssured.*; NOT working

Viewed 19184

I am trying to write a BDD framework for API Testing. import static io.restassured.RestAssured.*; NOT working. Also, when I use the given() keyword it throws error. I have added the respective maven dependencies and the static import is NOT working. Not understanding what needs to be added or the added dependencies needs to be changed.

Code Snippet: package restApi;

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>
7 Answers

I can see that you have set the scope of restassured limited to test in pom.xml

"<scope>test</scope>"

This means you would not be able to use the library anywhere else than tests. Get rid of it to make the scope default to project. i.e.

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

Use

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

instead of

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

Add the following inside your tests:

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

This may resolve some of the issues too.

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>

If given() is throwing compile time then use RestAssured.given().

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

If you have added the dependency and still you are Getting Error

Simply Build your Project .

Path (mac user ) Steps :

  1. project ->

  2. Build Project.

import static io.restassured.RestAssured.*;

This should be declared above all other imports, like this:

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