TransWikia.com

Rest Assured | Why getting repeated errors when trying to automate apis using rest assured?

Software Quality Assurance & Testing Asked by Rohan Kalia on November 21, 2021

I have just set up rest assured framework and implemented it as a maven project. My pom.xml has the below mentioned dependencies

  <dependencies>
  <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.0.2</version>
    <scope>test</scope>
</dependency>
  
  
  <!-- To parse json Document -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-path</artifactId>
    <version>3.0.2</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>


<!-- To parse xml document -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>xml-path</artifactId>
    <version>3.0.2</version>
</dependency>


<!-- Testing framework -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.10</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest Used for Regex -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>java-hamcrest</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>
  
</dependencies>  
</project>

And my code looks like

import static io.restassured.RestAssured.*;

import org.testng.annotations.Test;

public class LrnAsServiceApis {
    

    @Test
    public void trackvialrnumber() {
        
        given().get("lt-api.delta.com/v1/fulltrack/ABCD").then().log().all();
    }

But when I run it as TestNg I get error: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

And when I try to fix this by adding groovy all dependency I get another error:

java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase

And when I add another dependency, I get another error and this becomes never ending and frustrating. Is it that cumbersome? How do I fix once and for all?

One Answer

The Problem is with scope of the Rest Assured dependency in the pom.xml file

If your running your Rest Assured test from src/main/java the scope should be compile and your dependency will be as shown below

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

and if your running your Rest Assured test from src/test/java the scope should be test and your dependency will be as shown below

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

Solution is to change the scope to compile for rest assured dependency in pom.xml file

Reference: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

compile:This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

test:This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.

Answered by Mohamed Sulaimaan Sheriff on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP