rest-assured: The import io.restassured.RestAssured cannot be resolved

Hi Johan,

I am getting the red squiggly lines when I use the below static imports: import static io.restassured.RestAssured.*; import static io.restassured.matcher.RestAssuredMatchers.*;

My POM. xml has the below dependencies:

	<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>rest-assured</artifactId>
		<version>3.0.3</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.3</version>
	</dependency>

	<!-- to validate that a JSON response conforms to a Json Schema -->
	<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.3</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
		<version>4.5.3</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpmime</artifactId>
		<version>4.5.3</version>
	</dependency>

	<dependency>
		<groupId>org.hamcrest</groupId>
		<artifactId>java-hamcrest</artifactId>
		<version>2.0.0.0</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
	<dependency>
		<groupId>org.hamcrest</groupId>
		<artifactId>hamcrest-core</artifactId>
		<version>1.3</version>
		<scope>test</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
	<dependency>
		<groupId>org.hamcrest</groupId>
		<artifactId>hamcrest-library</artifactId>
		<version>1.3</version>
		<scope>test</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
	<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.5</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.testng/testng -->
	<dependency>
		<groupId>org.testng</groupId>
		<artifactId>testng</artifactId>
		<version>6.11</version>
		<scope>test</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
	<dependency>
		<groupId>com.beust</groupId>
		<artifactId>jcommander</artifactId>
		<version>1.72</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
	<dependency>
		<groupId>org.yaml</groupId>
		<artifactId>snakeyaml</artifactId>
		<version>1.18</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
	<dependency>
		<groupId>com.google.inject</groupId>
		<artifactId>guice</artifactId>
		<version>4.1.0</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
	<dependency>
		<groupId>org.assertj</groupId>
		<artifactId>assertj-core</artifactId>
		<version>3.8.0</version>
		<scope>test</scope>
	</dependency>

And my imports for the time being are: import static io.restassured.RestAssured.; import static io.restassured.matcher.RestAssuredMatchers.; import static org.hamcrest.Matchers.; import java.io.IOException; import java.io.InputStream; import java.util.List; import static io.restassured.path.json.JsonPath.; import org.testng.annotations.Test; import io.restassured.path.json.JsonPath;

Don’t know where am I missing something. Also the given() etc (without any parameter) is not showing in the IntelliSense. Please help!!

image

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 13
  • Comments: 53

Most upvoted comments

The reason is the <scope>test</scope>

The fix for version 4.1.1 is to add some transitive dependencies:

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured-common</artifactId>
            <version>4.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <version>4.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>xml-path</artifactId>
            <version>4.1.1</version>
            <scope>test</scope>
        </dependency>

They are now excluded in the maven-bundle-plugin rest-assured 4.1.1 configuration. See the negated packages in the <Export-Package> of https://repo.maven.apache.org/maven2/io/rest-assured/rest-assured/4.1.1/rest-assured-4.1.1.pom

I’m having this same problem.

@ramanojha Maven 4.0.0, Eclipse Neon, RestAssured 3.0.0 (scope test).

I don’t think RestAssured.given() is necessary. If you import it like I do, such as import static io.restassured.RestAssured.given;, then you can use just given() and know exactly where it comes from.

remove <scope>test</scope> from your pom dependency and it will work. as Suggested above by r-simlinger. It is working for me.

Removing the scope as test could solve it, but its not the right thing to do. Follow the solution suggested by @oharsta

In rest assured versions above 4.x, explicit packages are excluded.

           <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.5.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            !io.restassured.path.xml.*,
                            !io.restassured.path.json.*,
                            !io.restassured.common.*,
                            !io.restassured.internal.path.xml.*,
                            !io.restassured.internal.path.json.*,
                            !io.restassured.internal.common.*,
                            io.restassured.*
                        </Export-Package>
                        <Import-Package>
                            groovy.*;version="${groovy.range}",
                            org.codehaus.groovy.*;version="${groovy.range}",
                            *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>

And so, you need to explicitly provide certain entries which are excluded.

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>xml-path</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

Solved this error by using earlier versions of Rest Assured and TestNG. Also added dependency for groovy-all. So my current POM is:

<dependency>
			<groupId>io.rest-assured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>3.0.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.9.10</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>2.4.5</version>
		</dependency>

Please tell what are the other dependencies should be there when using rest-assured 3.0.3 or can you tell how to know all the compile time dependencies which are going to be needed for using version 3.0.3 of Rest Assured. Thanks!

nice it works after remove scope

Removing <scope>test</scope> worked for me

I had this problem for a couple of years, last time I just bailed on 3.x. This time I pushed thru as indicated above this is maven jar file problem, goto the command line and try this:

mvn clean test-compile

see if you get something like this:

invalid LOC header (bad signature)

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] error reading /opt/.m2/repository/io/rest-assured/rest-assured/3.3.0/rest-assured-3.3.0.jar; invalid LOC header (bad signature)
[ERROR] error reading /opt/.m2/repository/org/codehaus/groovy/groovy/2.4.15/groovy-2.4.15.jar; invalid LOC header (bad signature)

I had this issue for both rest-assured and groovy, I assume they are related. I moved the entire rest-assured and groovy dirs to a new name to force a re-download and it works!!!

No need to change scope.

I saved the original non-working m2 sub dirs, @johanhaleby let me know if you need any input

Do not remove the test scope. You do not want test classes in your production JARs.

I am using Kotlin with Java > 9 so my test dependencies look as follows

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured-all</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

I got back my RestAssured.* classes

Removed test from scope it worked for me in 4.3.0 @all

I am also getting the same error in eclipse. Can I still go add the dependencies in pom.xml? does it require for projects in eclipse?

Try running maven->update if your are using eclipse. Select project - right click - maven - update

Try to ignore the version :

<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>rest-assured</artifactId>
		<scope>test</scope>
</dependency>

I would like to suggest please continue with RestAssured.given() instead of just given() RestAssured. given(). get(url). getStatusCode();

I have face same issue and i tried all the soultion mentioned here …however still am having problem with given(). is not recognized I am using 3.0.0

io.rest-assured rest-assured ${restAssuredVersion}

io.rest-assured json-path ${restAssuredVersion}

io.rest-assured xml-path ${restAssuredVersion}

io.rest-assured json-schema-validator ${restAssuredVersion}

suthakar79 make sure you have this line import static io.restassured.RestAssured.*; in ur step definition file